<?php

namespace App\Filament\Widgets;

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

class FinancialMetricsWidget extends BaseWidget
{
    protected static ?int $sort = 2;

    // Only render where explicitly registered — not on the global dashboard
    protected static bool $isDiscovered = false;

    protected function getStats(): array
    {
        $inventoryCapital = Product::selectRaw('SUM(COALESCE(cost_price, 0) * COALESCE(quantity, 0)) as total_cost')
            ->value('total_cost') ?? 0.00;

        $potentialRevenue = Product::selectRaw('SUM(COALESCE(price, 0) * COALESCE(quantity, 0)) as total_retail')
            ->value('total_retail') ?? 0.00;

        $potentialProfit = max(0, $potentialRevenue - $inventoryCapital);

        return [
            Stat::make('Inventory Value (Cost)', '৳' . number_format($inventoryCapital, 2))
                ->description('Capital tied up in current stock')
                ->descriptionIcon('heroicon-m-banknotes')
                ->color('primary'),
            Stat::make('Potential Revenue', '৳' . number_format($potentialRevenue, 2))
                ->description('Estimated value of stock at retail')
                ->descriptionIcon('heroicon-m-arrow-trending-up')
                ->color('info'),
            Stat::make('Potential Profit', '৳' . number_format($potentialProfit, 2))
                ->description('Profit if all inventory is sold')
                ->descriptionIcon('heroicon-m-sparkles')
                ->color('success'),
        ];
    }
}
