<?php

namespace App\Filament\Employee\Widgets;

use App\Models\AttendanceCorrection;
use App\Models\EarlyLeaveRequest;
use App\Models\LateArrivalRequest;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;

class MyRequestsWidget extends BaseWidget
{
    protected static ?string $pollingInterval = '60s';

    protected static ?int $sort = 2;

    protected int|string|array $columnSpan = 'full';

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

        // My pending requests
        $pendingCorrections = AttendanceCorrection::where('user_id', $user->id)
            ->where('status', 'pending')
            ->count();

        $pendingLateArrival = LateArrivalRequest::where('user_id', $user->id)
            ->where('status', 'pending')
            ->count();

        $pendingEarlyLeave = EarlyLeaveRequest::where('user_id', $user->id)
            ->where('status', 'pending')
            ->count();

        $totalPending = $pendingCorrections + $pendingLateArrival + $pendingEarlyLeave;

        // Approved this month
        $approvedCorrections = AttendanceCorrection::where('user_id', $user->id)
            ->where('status', 'approved')
            ->whereMonth('approved_at', $thisMonth)
            ->whereYear('approved_at', $thisYear)
            ->count();

        $approvedLateArrival = LateArrivalRequest::where('user_id', $user->id)
            ->where('status', 'approved')
            ->whereMonth('approved_at', $thisMonth)
            ->whereYear('approved_at', $thisYear)
            ->count();

        $approvedEarlyLeave = EarlyLeaveRequest::where('user_id', $user->id)
            ->where('status', 'approved')
            ->whereMonth('approved_at', $thisMonth)
            ->whereYear('approved_at', $thisYear)
            ->count();

        $totalApproved = $approvedCorrections + $approvedLateArrival + $approvedEarlyLeave;

        // Quota usage
        $company = $user->company;
        $correctionQuotaUsed = $this->getQuotaUsed($user, 'correction');
        $correctionQuotaMax = $company->correction_quota_max;
        $lateArrivalQuotaUsed = $this->getQuotaUsed($user, 'late_arrival');
        $lateArrivalQuotaMax = $company->late_arrival_quota_max;
        $earlyLeaveQuotaUsed = $this->getQuotaUsed($user, 'early_leave');
        $earlyLeaveQuotaMax = $company->early_leave_quota_max;

        return [
            Stat::make('Pengajuan Menunggu', $totalPending)
                ->description($pendingCorrections . ' koreksi, ' . $pendingLateArrival . ' terlambat, ' . $pendingEarlyLeave . ' pulang cepat')
                ->descriptionIcon('heroicon-m-clock')
                ->color($totalPending > 0 ? 'warning' : 'success')
                ->extraAttributes([
                    'class' => 'cursor-pointer',
                ]),

            Stat::make('Disetujui Bulan Ini', $totalApproved)
                ->description($approvedCorrections . ' koreksi, ' . $approvedLateArrival . ' terlambat, ' . $approvedEarlyLeave . ' pulang cepat')
                ->descriptionIcon('heroicon-m-check-circle')
                ->color('success')
                ->extraAttributes([
                    'class' => 'cursor-pointer',
                ]),

            Stat::make('Kuota Koreksi', $correctionQuotaUsed . ($correctionQuotaMax ? ' / ' . $correctionQuotaMax : ' / Unlimited'))
                ->description($correctionQuotaMax ? ($correctionQuotaMax - $correctionQuotaUsed) . ' tersisa' : 'Tidak terbatas')
                ->descriptionIcon($correctionQuotaMax && $correctionQuotaUsed >= $correctionQuotaMax ? 'heroicon-m-exclamation-triangle' : 'heroicon-m-check-circle')
                ->color($correctionQuotaMax && $correctionQuotaUsed >= $correctionQuotaMax ? 'danger' : 'info')
                ->extraAttributes([
                    'class' => 'cursor-pointer',
                ]),

            Stat::make('Kuota Izin', ($lateArrivalQuotaUsed + $earlyLeaveQuotaUsed) . ($lateArrivalQuotaMax || $earlyLeaveQuotaMax ? ' / ' . ($lateArrivalQuotaMax + $earlyLeaveQuotaMax) : ' / Unlimited'))
                ->description('Terlambat: ' . $lateArrivalQuotaUsed . ($lateArrivalQuotaMax ? '/' . $lateArrivalQuotaMax : '/∞') . ', Pulang Cepat: ' . $earlyLeaveQuotaUsed . ($earlyLeaveQuotaMax ? '/' . $earlyLeaveQuotaMax : '/∞'))
                ->descriptionIcon('heroicon-m-calendar')
                ->color('info')
                ->extraAttributes([
                    'class' => 'cursor-pointer',
                ]),
        ];
    }

    private function getQuotaUsed($user, string $type): int
    {
        $thisMonth = now()->month;
        $thisYear = now()->year;

        return match($type) {
            'correction' => AttendanceCorrection::where('user_id', $user->id)
                ->whereIn('status', ['pending', 'approved', 'auto_approved'])
                ->whereMonth('created_at', $thisMonth)
                ->whereYear('created_at', $thisYear)
                ->count(),
            'late_arrival' => LateArrivalRequest::where('user_id', $user->id)
                ->whereIn('status', ['pending', 'approved', 'used'])
                ->whereMonth('created_at', $thisMonth)
                ->whereYear('created_at', $thisYear)
                ->count(),
            'early_leave' => EarlyLeaveRequest::where('user_id', $user->id)
                ->whereIn('status', ['pending', 'approved', 'used'])
                ->whereMonth('created_at', $thisMonth)
                ->whereYear('created_at', $thisYear)
                ->count(),
            default => 0,
        };
    }
}
