<?php

namespace App\Filament\Widgets;

use App\Models\Activity;
use App\Models\CashRecord;
use App\Models\User;
use Filament\Widgets\StatsOverviewWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;

class StatsOverview extends StatsOverviewWidget
{
    protected function getStats(): array
    {
        $totalKas = CashRecord::whereNotNull('verified_at')
            ->join('cash_categories', 'cash_records.category_id', '=', 'cash_categories.id')
            ->selectRaw("SUM(CASE WHEN cash_categories.type = 'pemasukan' THEN amount ELSE -amount END) as saldo")
            ->value('saldo') ?? 0;

        return [
            Stat::make('Anggota Aktif', User::where('status', 'aktif')->count())
                ->icon('heroicon-o-users')
                ->color('success'),

            Stat::make('Total Kas', 'Rp ' . number_format($totalKas, 0, ',', '.'))
                ->icon('heroicon-o-banknotes')
                ->color($totalKas >= 0 ? 'success' : 'danger'),

            Stat::make('Kegiatan Berjalan', Activity::where('status', 'approved')
                ->whereNull('executed_at')->count())
                ->icon('heroicon-o-calendar-days')
                ->color('info'),

            Stat::make('Kegiatan Pending', Activity::where('status', 'pending')->count())
                ->icon('heroicon-o-clock')
                ->color('warning'),
        ];
    }
}
