<?php

namespace App\Exports;

use App\Models\Appointments;
use App\Models\Especiality;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Illuminate\Support\Carbon;
use DateTime;

class AppointmentExport implements FromCollection, WithHeadings, WithMapping, WithStyles, ShouldAutoSize
{
    protected $today;
    protected $day;
    protected $myAppointments;
    protected $userId;

    public function __construct($params = [])
    {
        $this->today = isset($params['today']) ? $params['today'] : false;
        $this->day = isset($params['day']) ? $params['day'] : null;
        $this->myAppointments = isset($params['myAppointments']) ? $params['myAppointments'] : false;
        $this->userId = isset($params['userId']) ? $params['userId'] : null;
    }

    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        $query = Appointments::where("active", '=', '1');
        
        if ($this->today) {
            $query->whereDate("date", Carbon::today());
        } elseif ($this->day) {
            $query->whereDate("date", Carbon::parse($this->day));
        } elseif ($this->myAppointments && $this->userId) {
            $query->where("users_id", '=', $this->userId);
        }
        
        return $query->with(['patient', 'user'])->get();
    }
    
    /**
     * @return array
     */
    public function headings(): array
    {
        return [
            'Código',
            'Paciente',
            'Especialidad',
            'Doctor',
            'Fecha',
            'Hora',
            'Resumen'
        ];
    }
    
    /**
     * @param mixed $appointment
     *
     * @return array
     */
    public function map($appointment): array
    {
        $patient = $appointment->patient()->first();
        $date = new DateTime($appointment->date);
        $especialidades = Especiality::getEspecialities();
        
        return [
            $appointment->code,
            $patient ? $patient->firstname . ' ' . $patient->lastname : 'N/A',
            isset($especialidades[$appointment->especiality]) ? $especialidades[$appointment->especiality] : 'N/A',
            $appointment->user ? $appointment->user->name : 'N/A',
            $date->format('d/m/Y'),
            $date->format('h:i A'),
            $appointment->summary
        ];
    }
    
    /**
     * @param Worksheet $sheet
     *
     * @return void
     */
    public function styles(Worksheet $sheet)
    {
        return [
            // Estilo para la primera fila (encabezados)
            1 => [
                'font' => ['bold' => true],
                'fill' => [
                    'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
                    'startColor' => ['rgb' => '4A90E2']
                ],
                'font' => [
                    'color' => ['rgb' => 'FFFFFF']
                ]
            ]
        ];
    }
}
