<?php

namespace App\Filament\Widgets;

use App\Models\Visit;
use Filament\Widgets\Widget;
use Illuminate\Support\Carbon;

class CustomCalendarWidget extends Widget
{
    protected static string $view = 'filament.widgets.custom-calendar-widget';
    protected int|string|array $columnSpan = 'full';

    public function getEvents(): array
    {
        return Visit::query()
            ->with('patient')
            ->get()
            ->map(function (Visit $visit) {
            return [
                'id' => $visit->id,
                'title' => ($visit->patient ? $visit->patient->name : 'Unknown') . ' - ' . $visit->type,
                'start' => Carbon::parse($visit->visit_date)->toIso8601String(),
                'end' => Carbon::parse($visit->visit_date)->addMinutes(30)->toIso8601String(),
                'color' => $visit->status === 'Scheduled' ? '#3b82f6' : '#6b7280',
                'url' => route('filament.admin.resources.visits.edit', ['record' => $visit->id]),
            ];
        })
            ->toArray();
    }
}
