<?php

namespace App\Http\Livewire;

use LaravelViews\Views\DetailView;
use \App\Models\History;
use \App\Models\SectionHistory;
use App\Models\Especiality;
use App\Models\SectionType;
use App\Models\HistoryConfiguration;

class HistoryDetailView extends DetailView
{
    protected $modelClass = History::class;


    /**
     * @param $model Model instance
     * @return Array Array with all the detail data or the components
     */
    public function detail($model)
    {
        $especialities = Especiality::getEspecialities();

        // Datos básicos de la historia
        $dataHistory = [   
            __("common-view.view.numberHistory") => $model->number_history,
            __("common-view.view.firstname") => $model->patient()->first()->firstname ." ".$model->patient()->first()->lastname,
            __("common-view.view.createdBy") => $model->user()->first()->name,
            __("common-view.view.especiality") => $especialities[$model->especiality],
            __("common-view.view.reason") => $model->title,
            __("Cie") => $model->cie_code,
            __("common-view.view.diagnosis") =>$model->diagnosis, 
            __("common-view.view.currentIllnesses") => $model->summary,
            __("common-view.view.createdAt") => $model->created_at
        ];
        
        // Obtener configuración personalizada si existe
        $configuration = HistoryConfiguration::where('especiality_id', $model->especiality)->first();
        
        // Procesar antecedentes (secciones)
        $dataSections = [];
        $sectionHistories = $model->getSectionHistory();
        
        foreach ($sectionHistories as $sectionHistory) {
            // Verificar si debemos mostrar esta sección según la configuración
            if ($configuration && !empty($configuration->section_types)) {
                if (!in_array($sectionHistory->section_type_id, $configuration->section_types)) {
                    continue; // Omitir secciones que no están en la configuración
                }
            }
            
            // Obtener el nombre de la sección
            $sectionType = SectionType::find($sectionHistory->section_type_id);
            if ($sectionType) {
                $sectionName = __("backgrounds.".$sectionType->value);
                $dataSections[$sectionName] = $sectionHistory->summary;
            }
        }
        
        // Procesar exámenes físicos
        $dataPhysicalTest = [];
        $physicalExam = $model->getPhyscalExam();
        
        if (!empty($physicalExam)) {
            foreach ($physicalExam as $key => $value) {
                // Verificar si debemos mostrar este examen según la configuración
                if ($configuration && !empty($configuration->medical_exams)) {
                    if (!in_array($key, $configuration->medical_exams)) {
                        continue; // Omitir exámenes que no están en la configuración
                    }
                }
                
                // Solo incluir los valores que no estén vacíos
                if (!empty($value)) {
                    $examName = __("com-form-history.".$key);
                    $dataPhysicalTest[$examName] = $value;
                }
            }
        }

        return array_merge(
            $dataHistory,
            $dataSections,
            $dataPhysicalTest
        );
    }
}
