<?php

namespace App\Filament\Widgets;

use App\Models\Invoice;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;

class InvoiceStatsOverview extends BaseWidget
{
    protected function getStats(): array
    {
        $currency = auth()->user()->business->currency ?? 'TZS';

        $totalInvoiced = Invoice::where('status', '!=', 'cancelled')->sum('total');

        $paid = Invoice::where('status', 'paid')->sum('total');

        $outstanding = Invoice::whereIn('status', ['sent', 'overdue'])->sum('total');

        $overdue = Invoice::where('status', 'sent')
            ->where('due_date', '<', now())
            ->sum('total');

        $format = fn (int $cents) => $currency . ' ' . number_format($cents / 100, 2);

        return [
            Stat::make('Total Invoiced', $format($totalInvoiced)),
            Stat::make('Paid', $format($paid))
                ->color('success'),
            Stat::make('Outstanding', $format($outstanding))
                ->color('warning'),
            Stat::make('Overdue', $format($overdue))
                ->color('danger'),
        ];
    }
}