<?php

namespace App\Filament\Widgets;

use App\Models\Customer;
use App\Models\DelayedGood;
use App\Models\EggCollection;
use App\Models\Expense;
use App\Models\FeedPurchase;
use App\Models\Order;
use BezhanSalleh\FilamentShield\Traits\HasWidgetShield;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;

class StatsOverview extends BaseWidget
{
    use HasWidgetShield;

    protected static ?int $sort = 1;

    protected function getStats(): array
    {
        return [
            Stat::make('Produksi Telur', number_format(EggCollection::sum('total_eggs'), 0, ',', '.'))
                ->description('Total akumulasi produksi telur (berbagai satuan).')
                ->descriptionIcon('heroicon-m-circle-stack')
                ->color('warning'),

            Stat::make('Order', number_format(Order::count(), 0, ',', '.').' Pesanan')
                ->description('Jumlah pesanan yang dibuat.')
                ->descriptionIcon('heroicon-m-shopping-cart')
                ->color('info'),

            Stat::make('Pendapatan', 'Rp '.number_format(Order::sum('total_amount'), 0, ',', '.'))
                ->description('Total pendapatan dari pesanan.')
                ->descriptionIcon('heroicon-m-banknotes')
                ->color('success'),

            Stat::make('Total Pelanggan', number_format(Customer::count(), 0, ',', '.').' Orang')
                ->description('Total pelanggan terdaftar.')
                ->descriptionIcon('heroicon-m-users')
                ->color('primary'),

            Stat::make('Total Pengeluaran', 'Rp '.number_format(Expense::sum('amount'), 0, ',', '.'))
                ->description('Total akumulasi biaya pengeluaran.')
                ->descriptionIcon('heroicon-m-credit-card')
                ->color('danger'),

            Stat::make('Barang Tertunda', number_format(DelayedGood::count(), 0, ',', '.').' Item')
                ->description('Jumlah barang yang masih tertunda.')
                ->descriptionIcon('heroicon-m-clock')
                ->color('warning'),

            Stat::make('Total Belanja Pakan', 'Rp '.number_format(FeedPurchase::sum('total_price'), 0, ',', '.'))
                ->description('Total pengeluaran untuk pakan.')
                ->descriptionIcon('heroicon-m-shopping-bag')
                ->color('info'),

            Stat::make('Laba Kotor', 'Rp '.number_format(Order::sum('total_amount') - (Expense::sum('amount') + FeedPurchase::sum('total_price')), 0, ',', '.'))
                ->description('Pendapatan - (Pengeluaran + Pakan).')
                ->descriptionIcon('heroicon-m-presentation-chart-line')
                ->color('success'),

            Stat::make('Piutang Belum Terbayar', 'Rp '.number_format(DelayedGood::where('is_paid', false)->sum('total_amount') - DelayedGood::where('is_paid', false)->sum('paid_amount'), 0, ',', '.'))
                ->description('Total piutang dari barang tertunda.')
                ->descriptionIcon('heroicon-m-receipt-percent')
                ->color('danger'),

            Stat::make('Total Biaya Operasional', 'Rp '.number_format(Expense::sum('amount') + FeedPurchase::sum('total_price'), 0, ',', '.'))
                ->description('Total beban biaya saat ini.')
                ->descriptionIcon('heroicon-m-calculator')
                ->color('warning'),
        ];
    }
}
