<?php

namespace App\Filament\Widgets\Executive;

use App\Models\Attendance;
use Carbon\Carbon;
use Filament\Widgets\ChartWidget;

class AttendanceTrendWidget extends ChartWidget
{
    protected static ?string $heading = 'Trend Kehadiran 30 Hari Terakhir';

    protected static ?string $description = 'Grafik kehadiran harian untuk analisis trend';

    protected static ?int $sort = 2;

    protected int | string | array $columnSpan = [
        'md' => 2,
        'xl' => 2,
    ];

    protected static ?string $maxHeight = '300px';

    protected function getData(): array
    {
        $labels = [];
        $presentData = [];
        $lateData = [];
        $absentData = [];

        for ($i = 29; $i >= 0; $i--) {
            $date = now()->subDays($i);
            $labels[] = $date->translatedFormat('d M');

            $presentData[] = Attendance::whereDate('date', $date->toDateString())
                ->where('status', 'present')
                ->whereHas('user', fn ($q) => $q->whereDoesntHave('roles', fn ($r) => $r->where('name', 'super_admin')))
                ->count();

            $lateData[] = Attendance::whereDate('date', $date->toDateString())
                ->where('status', 'late')
                ->whereHas('user', fn ($q) => $q->whereDoesntHave('roles', fn ($r) => $r->where('name', 'super_admin')))
                ->count();

            $absentData[] = Attendance::whereDate('date', $date->toDateString())
                ->whereIn('status', ['absent', 'incomplete'])
                ->whereHas('user', fn ($q) => $q->whereDoesntHave('roles', fn ($r) => $r->where('name', 'super_admin')))
                ->count();
        }

        return [
            'datasets' => [
                [
                    'label' => 'Tepat Waktu',
                    'data' => $presentData,
                    'backgroundColor' => 'rgba(34, 197, 94, 0.1)',
                    'borderColor' => 'rgb(34, 197, 94)',
                    'fill' => true,
                ],
                [
                    'label' => 'Terlambat',
                    'data' => $lateData,
                    'backgroundColor' => 'rgba(234, 179, 8, 0.1)',
                    'borderColor' => 'rgb(234, 179, 8)',
                    'fill' => true,
                ],
                [
                    'label' => 'Tidak Hadir',
                    'data' => $absentData,
                    'backgroundColor' => 'rgba(239, 68, 68, 0.1)',
                    'borderColor' => 'rgb(239, 68, 68)',
                    'fill' => true,
                ],
            ],
            'labels' => $labels,
        ];
    }

    protected function getType(): string
    {
        return 'line';
    }

    protected function getOptions(): array
    {
        return [
            'scales' => [
                'y' => [
                    'beginAtZero' => true,
                    'ticks' => [
                        'stepSize' => 1,
                    ],
                ],
            ],
            'plugins' => [
                'legend' => [
                    'display' => true,
                    'position' => 'top',
                ],
            ],
        ];
    }
}
