<?php

namespace App\Filament\Widgets;

use Filament\Widgets\StatsOverviewWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;
use App\Models\Aduan;

class AduanStats extends StatsOverviewWidget
{
    protected function getStats(): array
    {
        $total       = Aduan::count();
        $menunggu    = Aduan::where('status', 'menunggu')->count();
        $diproses    = Aduan::where('status', 'diproses')->count();
        $selesai     = Aduan::where('status', 'selesai')->count();
        $penting     = Aduan::where('prioritas', 'penting')->count();
        $avgScore    = Aduan::whereNotNull('confidence_score')->avg('confidence_score');
        $infrastruktur = Aduan::where('kategori', 'like', '%infrastruktur%')->count();
        $kebersihan  = Aduan::where('kategori', 'like', '%kebersihan%')->count();
        $pelayanan   = Aduan::where('kategori', 'like', '%pelayanan%')->count();
        $keamanan    = Aduan::where('kategori', 'like', '%keamanan%')->count();
        $sosial      = Aduan::where('kategori', 'like', '%sosial%')->count();
        $utilitas    = Aduan::where('kategori', 'like', '%utilitas%')->count();

        return [
            Stat::make('Total Aduan', $total)
                ->description('Seluruh aduan masuk')
                ->icon('heroicon-o-document-text')
                ->color('primary'),

            Stat::make('Menunggu', $menunggu)
                ->description('Belum ditangani')
                ->icon('heroicon-o-clock')
                ->color('warning'),

            Stat::make('Diproses', $diproses)
                ->description('Sedang ditangani')
                ->icon('heroicon-o-arrow-path')
                ->color('info'),

            Stat::make('Selesai', $selesai)
                ->description('Sudah ditindaklanjuti')
                ->icon('heroicon-o-check-circle')
                ->color('success'),

            Stat::make('Prioritas Penting', $penting)
                ->description('Aduan mendesak')
                ->icon('heroicon-o-exclamation-triangle')
                ->color('danger'),

            Stat::make('Avg. Confidence ML', $avgScore ? round($avgScore, 1) . '%' : 'N/A')
                ->description('Rata-rata akurasi klasifikasi Naive Bayes')
                ->icon('heroicon-o-cpu-chip')
                ->color('success'),

            Stat::make('Infrastruktur', $infrastruktur)
                ->description('Aduan infrastruktur & fasilitas')
                ->color('danger'),

            Stat::make('Kebersihan', $kebersihan)
                ->description('Aduan kebersihan & lingkungan')
                ->color('success'),

            Stat::make('Pelayanan', $pelayanan)
                ->description('Aduan pelayanan administrasi')
                ->color('primary'),

            Stat::make('Keamanan', $keamanan)
                ->description('Aduan keamanan & ketertiban')
                ->color('gray'),

            Stat::make('Sosial', $sosial)
                ->description('Aduan sosial & kesejahteraan')
                ->color('info'),

            Stat::make('Utilitas', $utilitas)
                ->description('Aduan layanan publik & utilitas')
                ->color('warning'),
        ];
    }
}
