<?php

namespace App\Filament\Widgets;

use App\Models\Company;
use App\Models\DataChangeRequest;
use App\Models\Journalist;
use App\Models\PartnerMedia;
use App\Models\VerificationRequest;
use App\Models\Visitor;
use BezhanSalleh\FilamentShield\Traits\HasWidgetShield;
use Filament\Support\Icons\Heroicon;
use Filament\Widgets\Concerns\InteractsWithPageFilters;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;

class OverviewStats extends BaseWidget
{
    use HasWidgetShield, InteractsWithPageFilters;

    protected static ?int $sort = 1;

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

    protected function getStats(): array
    {
        $startDate = $this->filters['startDate'] ?? null;
        $endDate = $this->filters['endDate'] ?? null;

        $totalJournalists = Journalist::when($startDate, fn ($q) => $q->whereDate('created_at', '>=', $startDate))
            ->when($endDate, fn ($q) => $q->whereDate('created_at', '<=', $endDate))
            ->count();

        $totalMedia = PartnerMedia::when($startDate, fn ($q) => $q->whereDate('created_at', '>=', $startDate))
            ->when($endDate, fn ($q) => $q->whereDate('created_at', '<=', $endDate))
            ->count();

        $average = $totalMedia > 0 ? round($totalJournalists / $totalMedia, 1) : 0;

        return [
            Stat::make('Total Perusahaan', Company::when($startDate, fn ($q) => $q->whereDate('created_at', '>=', $startDate))
                ->when($endDate, fn ($q) => $q->whereDate('created_at', '<=', $endDate))
                ->count())
                ->description('Perusahaan terdaftar')
                ->descriptionIcon(Heroicon::BuildingOffice2)
                ->color('primary'),

            Stat::make('Media Terverifikasi', PartnerMedia::whereHas('company.verificationRequest', function ($q) {
                $q->approved();
            })
                ->when($startDate, fn ($q) => $q->whereDate('created_at', '>=', $startDate))
                ->when($endDate, fn ($q) => $q->whereDate('created_at', '<=', $endDate))
                ->count())
                ->description('Media aktif')
                ->descriptionIcon(Heroicon::CheckBadge)
                ->color('success'),

            Stat::make('Total Jurnalis', Journalist::when($startDate, fn ($q) => $q->whereDate('created_at', '>=', $startDate))
                ->when($endDate, fn ($q) => $q->whereDate('created_at', '<=', $endDate))
                ->count())
                ->description('Jurnalis terdaftar')
                ->descriptionIcon(Heroicon::Identification)
                ->color('info'),

            Stat::make('Antrean Verifikasi', VerificationRequest::pending()
                ->when($startDate, fn ($q) => $q->whereDate('created_at', '>=', $startDate))
                ->when($endDate, fn ($q) => $q->whereDate('created_at', '<=', $endDate))
                ->count())
                ->description('Butuh tindakan')
                ->descriptionIcon(Heroicon::PencilSquare)
                ->color('warning'),

            Stat::make('Antrean Perubahan', DataChangeRequest::pending()
                ->when($startDate, fn ($q) => $q->whereDate('created_at', '>=', $startDate))
                ->when($endDate, fn ($q) => $q->whereDate('created_at', '<=', $endDate))
                ->count())
                ->description('Perubahan data')
                ->descriptionIcon(Heroicon::DocumentText)
                ->color('warning'),

            Stat::make('Total Pengunjung', Visitor::when($startDate, fn ($q) => $q->whereDate('date', '>=', $startDate))
                ->when($endDate, fn ($q) => $q->whereDate('date', '<=', $endDate))
                ->count())
                ->description('Total traffic')
                ->descriptionIcon(Heroicon::ChartBar)
                ->color('info'),

            Stat::make('Rata-rata Jurnalis / Media', $average)
                ->description('Rasio sebaran jurnalis')
                ->descriptionIcon(Heroicon::Users)
                ->color('primary'),
        ];
    }
}
