<?php

namespace App\Filament\Widgets\HRD;

use App\Models\AttendanceCorrection;
use App\Models\EarlyLeaveRequest;
use App\Models\LateArrivalRequest;
use Carbon\Carbon;
use Filament\Widgets\ChartWidget;

class RequestStatisticsWidget extends ChartWidget
{
    protected static ?string $heading = 'Statistik Pengajuan Bulan Ini';

    protected static ?string $description = 'Distribusi pengajuan berdasarkan jenis';

    protected static ?int $sort = 3;

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

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

    protected function getData(): array
    {
        $thisMonth = now()->month;
        $thisYear = now()->year;

        $corrections = AttendanceCorrection::whereMonth('created_at', $thisMonth)
            ->whereYear('created_at', $thisYear)
            ->count();

        $lateArrivals = LateArrivalRequest::whereMonth('created_at', $thisMonth)
            ->whereYear('created_at', $thisYear)
            ->count();

        $earlyLeaves = EarlyLeaveRequest::whereMonth('created_at', $thisMonth)
            ->whereYear('created_at', $thisYear)
            ->count();

        return [
            'datasets' => [
                [
                    'label' => 'Jumlah Pengajuan',
                    'data' => [$corrections, $lateArrivals, $earlyLeaves],
                    'backgroundColor' => [
                        'rgba(59, 130, 246, 0.8)',  // Info Blue - Corrections
                        'rgba(234, 179, 8, 0.8)',   // Warning Yellow - Late Arrival
                        'rgba(239, 68, 68, 0.8)',   // Danger Red - Early Leave
                    ],
                ],
            ],
            'labels' => ['Koreksi Absen', 'Izin Terlambat', 'Izin Pulang Cepat'],
        ];
    }

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