<?php

namespace App\Http\Livewire;

use LaravelViews\Views\DetailView;
use \App\Models\Patients;
use LaravelViews\Facades\UI;
use App\Models\Especiality;
use App\Models\RecurringAppointment;
use DateTime;
use App\Models\Settings;

class DetailPatientView extends DetailView
{

    protected $modelClass = Patients::class;



    /**
     * @param $model Model instance
     * @return Array Array with all the detail data or the components
     */
    public function detail($model)
    {
        $accessMode = Settings::getAccessMode();
        $address = [];
        $appointments = [];
        $recurringAppointments = [];
        $representatives = [];

        $especialities = Especiality::getEspecialities();
        $date  = new DateTime($model->birthday);

        //areas
        $areas = [];
        foreach ($model->getAreas() as $key => $value) {
            $class = "bg-success";
            $style = "";
            if ($key > 0) {
                $class = "bg-warning";
                $style = "background-color:#db3737 !important; color:#fff";
            }
            array_push($areas, '<span style="' . $style . '" class="badge ' . $class . '">' . $especialities[$value] . '</span>');
        }

        if (!isset($model->address)) {
            foreach (Patients::getListAddressOfRepresentatives($model) as $addr) {
                $address =  array_merge($address, [
                    __("common-view.view.address") . (count($address) + 1) => $addr
                ]);
            }
        } else {
            $address = [
                __("common-view.view.address") => $model->address
            ];
        }
        $address = array_merge($address, [
            __("common-view.view.phones") => (isset($model->phone)) ? '<span style="background-color:#4245A4 !important; color:#fff; font-size: 0.90em" class="badge bg-warning">' . $model->phone . '</span>' : '<span style="background-color:#db3737 !important; color:#fff; font-size: 0.90em" class="badge bg-warning">' . implode('</span> <span style="background-color:#db3737 !important; color:#fff; font-size: 0.90em" class="badge bg-warning">',Patients::getPhoneForRepresentatives($model)) . '</span>',
            __("common-view.view.emails") => (isset($model->email)) ? '<span style="background-color:#4245A4 !important; color:#fff; font-size: 0.90em" class="badge bg-warning">' . $model->email . '</span>' : '<span style="background-color:#db3737 !important; color:#fff; font-size: 0.90em" class="badge bg-warning">' . implode('</span> <span style="background-color:#db3737 !important; color:#fff; font-size: 0.90em" class="badge bg-warning">',Patients::getEmailForRepresentatives($model)) . '</span>'
        ]);

        //citas
        foreach ($model->appointments()->get() as $appointment) {
            if ($appointment->active) {
                array_push($appointments, '<span style="background-color:#4245A4 !important;" class="badge bg-danger badge-patient">' . UI::link("(" . $especialities[$appointment->especiality] . ") " . "-" . $appointment->code, "detail-appointment?id=" . $appointment->id) . '</span>');
            }
        }

        // Citas recurrentes
        $recurring = RecurringAppointment::where('patients_id', $model->id)
            ->where('is_active', true)
            ->with(['doctor', 'especialityRelation'])
            ->get();
        
        foreach ($recurring as $recApp) {
            $daysUntil = \Carbon\Carbon::now()->startOfDay()->diffInDays(\Carbon\Carbon::parse($recApp->next_appointment_date)->startOfDay(), false);
            $isPast = \Carbon\Carbon::parse($recApp->next_appointment_date)->isPast();
            $badgeClass = $isPast ? 'bg-danger' : ($daysUntil <= $recApp->reminder_days ? 'bg-warning' : 'bg-primary');
            $badgeStyle = $isPast ? 'background-color:#dc3545 !important;' : ($daysUntil <= $recApp->reminder_days ? 'background-color:#ffc107 !important; color:#212529;' : 'background-color:#4245A4 !important;');
            
            array_push($recurringAppointments, 
                '<span style="'.$badgeStyle.'" class="badge '.$badgeClass.' badge-patient">' . 
                UI::link(
                    "(" . $recApp->especialityRelation->name . ") - " . 
                    \Carbon\Carbon::parse($recApp->next_appointment_date)->format('d/m/Y') . 
                    " - " . $recApp->doctor->name, 
                    "recurring-appointments/show/" . $recApp->id
                ) . 
                '</span>'
            );
        }

        if ((int) $model->age < 18) {
            $representatives[__("common-view.view.representatives")] = Patients::getRepresentatives($model);
        }

        return array_merge(
            [
                __("common-view.view.identification") => $model->identification,
                __("common-view.view.age") =>  $model->age,
                __("common-view.view.birthday") => $date->format("d/m/Y"),
                __("common-view.view.areasHistory") => implode(" ", $areas),
                __("common-view.view.bloodType") => '<span style="color:#000; font-size: 0.90em" class="badge bg-danger">' . strtoupper($model->blood_type) . '</span>',
            ],
            (in_array(Settings::APPOINTMENT_ONLY_MODE, $accessMode) || in_array(Settings::FULL_ACCESS_MODE, $accessMode)) ? [
                __("common-view.view.appointmentsPedding") => implode(" ", $appointments),
                __("recurring-appointments.recurringAppointments") => !empty($recurringAppointments) ? implode(" ", $recurringAppointments) : __("recurring-appointments.noRecurringAppointments"),
            ] : [],
            $representatives,
            $address
        );
    }
}