<?php

namespace App\Filament\Widgets\HRD;

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

class AttendanceOverviewWidget extends ChartWidget
{
    protected static ?string $heading = 'Overview Kehadiran Mingguan';

    protected static ?string $description = 'Perbandingan status kehadiran 7 hari terakhir';

    protected static ?int $sort = 4;

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

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

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

        for ($i = 6; $i >= 0; $i--) {
            $date = now()->subDays($i);
            $labels[] = $date->translatedFormat('D, 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();

            $wfhData[] = Attendance::whereDate('date', $date->toDateString())
                ->where('type', 'wfh')
                ->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.8)',
                ],
                [
                    'label' => 'Terlambat',
                    'data' => $lateData,
                    'backgroundColor' => 'rgba(234, 179, 8, 0.8)',
                ],
                [
                    'label' => 'WFH',
                    'data' => $wfhData,
                    'backgroundColor' => 'rgba(59, 130, 246, 0.8)',
                ],
                [
                    'label' => 'Tidak Hadir',
                    'data' => $absentData,
                    'backgroundColor' => 'rgba(239, 68, 68, 0.8)',
                ],
            ],
            'labels' => $labels,
        ];
    }

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

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