<?php

namespace App\Filament\Employee\Widgets;

use App\Models\Attendance;
use App\Models\Activity;
use App\Models\BusinessTrip;
use Carbon\Carbon;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;

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

    protected static ?int $sort = 1;

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

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

        // Attendance this month
        $attendanceThisMonth = Attendance::where('user_id', $user->id)
            ->whereMonth('date', $thisMonth)
            ->whereYear('date', $thisYear)
            ->count();

        $presentDays = Attendance::where('user_id', $user->id)
            ->whereMonth('date', $thisMonth)
            ->whereYear('date', $thisYear)
            ->where('status', 'present')
            ->count();

        $lateDays = Attendance::where('user_id', $user->id)
            ->whereMonth('date', $thisMonth)
            ->whereYear('date', $thisYear)
            ->where('status', 'late')
            ->count();

        // Today's attendance
        $todayAttendance = Attendance::where('user_id', $user->id)
            ->whereDate('date', $today)
            ->first();

        $todayStatus = 'Belum Absen';
        $todayColor = 'warning';
        if ($todayAttendance) {
            if ($todayAttendance->check_out_time) {
                $todayStatus = 'Selesai';
                $todayColor = 'success';
            } elseif ($todayAttendance->check_in_time) {
                $todayStatus = 'Belum Checkout';
                $todayColor = 'info';
            }
        }

        // Activities this month
        $activitiesThisMonth = Activity::where('user_id', $user->id)
            ->whereMonth('date', $thisMonth)
            ->whereYear('date', $thisYear)
            ->count();

        // Business trips
        $businessTripsThisMonth = BusinessTrip::where('user_id', $user->id)
            ->whereMonth('start_date', $thisMonth)
            ->whereYear('start_date', $thisYear)
            ->count();

        // Calculate trend for attendance
        $attendanceTrend = [];
        for ($i = 6; $i >= 0; $i--) {
            $date = now()->subDays($i)->toDateString();
            $hasAttendance = Attendance::where('user_id', $user->id)
                ->whereDate('date', $date)
                ->whereIn('status', ['present', 'late'])
                ->exists();
            $attendanceTrend[] = $hasAttendance ? 1 : 0;
        }

        return [
            Stat::make('Status Hari Ini', $todayStatus)
                ->description($todayAttendance ? 'Check-in: ' . ($todayAttendance->check_in_time ? Carbon::parse($todayAttendance->check_in_time)->format('H:i') : '-') : 'Silakan absen')
                ->descriptionIcon($todayAttendance ? 'heroicon-m-check-circle' : 'heroicon-m-clock')
                ->color($todayColor),

            Stat::make('Kehadiran Bulan Ini', $presentDays . ' hari')
                ->description($lateDays > 0 ? $lateDays . ' hari terlambat' : 'Tidak ada keterlambatan')
                ->descriptionIcon($lateDays > 0 ? 'heroicon-m-exclamation-triangle' : 'heroicon-m-check-circle')
                ->color($lateDays > 0 ? 'warning' : 'success')
                ->chart($attendanceTrend),

            Stat::make('Aktivitas Bulan Ini', $activitiesThisMonth . ' aktivitas')
                ->description('Total aktivitas tercatat')
                ->descriptionIcon('heroicon-m-clipboard-document-list')
                ->color('info'),

            Stat::make('Perjalanan Dinas', $businessTripsThisMonth . ' perjalanan')
                ->description('Bulan ini')
                ->descriptionIcon('heroicon-m-paper-airplane')
                ->color('primary'),
        ];
    }
}
