<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Helper\Helper;
use Illuminate\Support\Facades\Mail;
use App\Mail\AppointmentNotificationMail;
use App\Mail\AppointmentMail;
use App\Http\Response\Response;
use App\Models\Representative;
use App\Models\Patients;
use App\Models\RelationShip;
use App\Models\Appointments;
use Illuminate\Support\Facades\DB;


class AppointmentController extends Controller
{
    /**
     * 
     */
    public function create(Request $request){
        $fields = $request->json()->all();
        try {
            //guardar en la base de datos
            DB::beginTransaction();
                $representative = Representative::factoryCreate($fields);
                $representative = Representative::create($representative);

                $patients = Patients::factoryCreate($fields);
                $patients = Patients::create($patients);

                $relationship = RelationShip::factoryCreate($representative->id,$patients->id,$fields);
                $relationship = RelationShip::create($relationship);

                $appointments = Appointments::factoryCreate($fields,$patients->id);
                $appointments = Appointments::create($appointments);

            //enviar notification por email al doctor
            $sendNotification = $this->sendNotification($fields);
            //enviar correo por email al representante
            $dispatchEmailAppointment = $this->dispatchEmailAppointment($patients,$appointments,$representative->email);
            $state = ((count($sendNotification) == 0)&&(count($dispatchEmailAppointment)==0));
            DB::commit();
            return Response::getResponseCreateAppointment($state,$appointments->code,200);
        } catch (\Throwable $th) {
            DB::rollback();
            return Response::getResponseCreateAppointment([false,strval($th)],NULL,500);
        }
    }

    /**
     * 
     */
    private function createTemplateEmail($fields){
        $body       = "<h1>Nueva consulta medica el ".$fields["appointmentDate"]." para ".Helper::getEspeciality($fields["especiality"]) . "</h1>";
        $body       .= "<br>";
        $body       .= "<br>";
        $body       .= "Paciente : ".$fields["patientFirstName"] ." ".$fields["patientLastName"];
        $body       .= "<br>";
        $body       .= "Edad : ".$fields["patientAge"];
        $body       .= "<br>";
        $body       .= "Fecha de Nacimiento :".$fields["birthday"];
        $body       .= "<br>";
        $body       .= "Sexo: ".$fields["patientGender"];
        $body       .= "<br>";
        $body       .= "<br>";
        $body       .= "Datos de quien solicita la consulta: ";
        $body       .= "<br>";
        $body       .= "Nombre: ".$fields["representativeFirstName"] ." ".$fields["representativeLastName"];
        $body       .= "<br>";
        $body       .= "edad: ".$fields["representativeAge"];
        $body       .= "<br>";
        $body       .= "Sexo: ".$fields["represenativeGender"];
        $body       .= "<br>";
        $body       .= "Parentezco con el paciente : es su ".Helper::getParent($fields["relationship"]);
        $body       .= "<br>";
        $body       .= "<br>";
        $body       .= "Datos de contacto:";
        $body       .= "<br>";
        $body       .= "Dirección: ".$fields["address"];
        $body       .= "<br>";
        $body       .= "Teléfono: ".$fields["phone"];
        $body       .= "<br>";
        $body       .= "Correo: ".$fields["mail"];

        return $body;
    }

    /**
     * 
     */
    private function sendNotification($fields){
        $emails     = [
            Helper::MAIL_TO_SEND,
            "yosai.max@gmail.com",
            "josefinav.dominguez@gmail.com"
        ];
        return Helper::sendEmail(Helper::MAIL_TO_SEND,$emails,"Nueva consulta médica",$fields,AppointmentNotificationMail::class);
    }

    /**
     * 
     */
    private function dispatchEmailAppointment($patient,$appointments,$representativeEmail){
        return Helper::sendEmail(
            Helper::MAIL_TO_SEND,
            [$representativeEmail],
            "Datos de su consulta médica",
            [
                "patient"=>$patient,
                "appointments"=>$appointments
            ],
            AppointmentMail::class); 
    }
    /**
     * 
     */
    private function sendEmail($fields){
        $from       = Helper::MAIL_TO_SEND;
        $emails     = [
            Helper::MAIL_TO_SEND,
            "yosai.max@gmail.com",
            "josefinav.dominguez@gmail.com"
        ];
        $subject    = "Nueva consulta médica";
         Mail::to($emails)->send(new AppointmentNotificationMail($from,$subject,$fields));
         return Mail::failures();
    }

    /**
     * 
     */
    public function getToken(){
        $token = csrf_token();
        return Response::getResponseToken($token,200);
    }

}
