<?php

namespace App\Filament\Widgets\Stats;

use App\Enums\IsActive;
use App\Enums\IssueSentiment;
use App\Models\Classification;
use App\Models\CooperationPayment;
use App\Models\DataChangeRequest;
use App\Models\Department;
use App\Models\IssueManagement;
use App\Models\Journalist;
use App\Models\Location;
use App\Models\PartnerMedia;
use App\Models\Theme;
use App\Models\VerificationRequest;
use App\Models\Visitor;
use BezhanSalleh\FilamentShield\Traits\HasWidgetShield;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;

class OverviewStats extends BaseWidget
{
    use HasWidgetShield;

    protected static ?int $sort = 1;

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

    protected ?string $pollingInterval = null;

    protected function getStats(): array
    {
        $totalJournalists = Journalist::count();
        $totalMedia = PartnerMedia::count();
        $average = $totalMedia > 0 ? round($totalJournalists / $totalMedia, 1) : 0;

        return [
            Stat::make('Total Klasifikasi', Classification::active()->count())
                ->description('Klasifikasi aktif')
                ->descriptionIcon('heroicon-o-book-open')
                ->color($this->getRandomColor()),

            Stat::make('Total Lokus', Location::active()->count())
                ->description('Lokus aktif')
                ->descriptionIcon('heroicon-o-map-pin')
                ->color($this->getRandomColor()),

            Stat::make('Total OPD', Department::where('is_active', IsActive::ACTIVE)->count())
                ->description('OPD aktif')
                ->descriptionIcon('heroicon-o-building-library')
                ->color($this->getRandomColor()),

            Stat::make('Total Tema', Theme::active()->count())
                ->description('Tema aktif')
                ->descriptionIcon('heroicon-o-puzzle-piece')
                ->color($this->getRandomColor()),

            Stat::make('Total Anggaran', 'Rp '.number_format(CooperationPayment::sum('amount'), 0, ',', '.'))
                ->description('Realisasi pembayaran')
                ->descriptionIcon('heroicon-o-banknotes')
                ->color('success'),

            Stat::make('Rasio Jurnalis', $average.' per Media')
                ->description('Rata-rata jurnalis')
                ->descriptionIcon('heroicon-o-users')
                ->color($this->getRandomColor()),

            Stat::make('Total Jurnalis', $totalJournalists)
                ->description('Jurnalis terdaftar')
                ->descriptionIcon('heroicon-o-identification')
                ->color($this->getRandomColor()),

            Stat::make('Isu Kritis', IssueManagement::whereIn('issue', [IssueSentiment::NEGATIVE, IssueSentiment::CRISIS])->count())
                ->description('Perlu perhatian')
                ->descriptionIcon('heroicon-o-exclamation-triangle')
                ->color('danger'),

            Stat::make('Antrean Verifikasi', VerificationRequest::pending()->count())
                ->description('Butuh tindakan')
                ->descriptionIcon('heroicon-o-pencil-square')
                ->color($this->getRandomColor()),

            Stat::make('Antrean Perubahan', DataChangeRequest::pending()->count())
                ->description('Perubahan data')
                ->descriptionIcon('heroicon-o-document-text')
                ->color($this->getRandomColor()),

            Stat::make('Total Pengunjung', Visitor::count())
                ->description('Total traffic')
                ->descriptionIcon('heroicon-o-chart-bar')
                ->color($this->getRandomColor()),
        ];
    }

    private array $availableColors = [
        'primary',
        'success',
        'warning',
        'danger',
        'info',
        'fuchsia',
        'indigo',
        'violet',
        'orange',
        'cyan',
        'emerald',
        'amber',
        'rose',
        'lime',
    ];

    private function getRandomColor(): string
    {
        return $this->availableColors[array_rand($this->availableColors)];
    }
}
