<?php

namespace App\Filament\Resources\Resellers\Widgets;

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

class ResellerStatsWidget extends BaseWidget
{
    public ?User $record = null;

    protected function getStats(): array
    {
        if (! $this->record) {
            return [];
        }

        $balance = $this->record->getWalletBalance();
        $totalOrders = $this->record->resellerOrders()->count();
        $totalSales = $this->record->resellerOrders()->sum('subtotal');
        $totalProfit = $this->record->resellerOrders()->sum('total_reseller_profit');
        $totalWithdrawn = $this->record->withdrawRequests()->where('status', 'approved')->sum('amount');

        return [
            Stat::make('Wallet Balance', '৳' . number_format($balance, 2))
                ->description('Current available balance')
                ->color('success')
                ->icon('heroicon-m-wallet'),
            Stat::make('Total Sales Volume', '৳' . number_format($totalSales, 2))
                ->description("From {$totalOrders} total orders")
                ->color('primary')
                ->icon('heroicon-m-shopping-cart'),
            Stat::make('Total Profit Earned', '৳' . number_format($totalProfit, 2))
                ->description('Life time profit credited')
                ->color('success')
                ->icon('heroicon-m-arrow-trending-up'),
            Stat::make('Total Withdrawn', '৳' . number_format($totalWithdrawn, 2))
                ->description('Total payout completed')
                ->color('warning')
                ->icon('heroicon-m-banknotes'),
        ];
    }
}
