<?php

namespace App\Filament\Widgets;

use App\Models\RequestQuotation;
use App\Models\RfqTransporterBid;
use Filament\Facades\Filament;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;

class RfqChart extends BaseWidget
{
    // protected function getStats(): array
    // {
    //     $transporter = session('transport_name');
    //     $rfqNumber   = session('rfq_number');

    //     if (!$transporter || !$rfqNumber) {
    //         return [
    //             Stat::make('Total Freight Charge', '-'),
    //             Stat::make('Rank', '-'),
    //         ];
    //     }

    //     $selectedRfq = RequestQuotation::query()
    //         ->where('transporter_name', $transporter)
    //         ->where('rfq_number', $rfqNumber)
    //         ->first();

    //     if (!$selectedRfq) {
    //         return [
    //             Stat::make('Total Freight Charge', '-'),
    //             Stat::make('Rank', '-'),
    //         ];
    //     }

    //     $myAmount = (float) $selectedRfq->total_freight_charge;

    //     $rank = RequestQuotation::query()
    //         ->whereRaw(
    //             'CAST(total_freight_charge AS DECIMAL(10,2)) < ?',
    //             [$myAmount]
    //         )
    //         ->selectRaw('CAST(total_freight_charge AS DECIMAL(10,2))')
    //         ->distinct()
    //         ->count() + 1;

    //         $medal = match (true) {
    //             $rank == 1 => '🥇',
    //             $rank == 2 => '🥈',
    //             $rank == 3 => '🥉',
    //             default     => '',
    //         };

    //     return [
    //         Stat::make(
    //             'Total Freight Charge',
    //             number_format($selectedRfq->total_freight_charge, 2)
    //         )
    //             ->description('Transporter: ' . $selectedRfq->transporter_name)
    //             ->color($rank == 1 ? 'success' : 'primary'),

    //         Stat::make(
    //             'Rank',
    //             trim("{$medal} #{$rank}")
    //         )
    //         ->description('Among all transporters')
    //         ->color(
    //             $rank == 1 ? 'success' :
    //             ($rank <= 3 ? 'warning' : 'gray')
    //         ),
    //     ];
    // }

    protected function getStats(): array
    {
        $transporter = session('transport_name');
        $rfqNumber   = session('rfq_id');

        if (!$transporter || !$rfqNumber) {
            return [
                Stat::make('Total Freight Charge', '-'),
                Stat::make('Rank', '-'),
            ];
        }

        $selectedRfq = RfqTransporterBid::query()
            ->where('transporter_name', $transporter)
            ->where('request_quotation_id', $rfqNumber)
            ->first();

        if (!$selectedRfq) {
            return [
                Stat::make('Total Freight Charge', '-'),
                Stat::make('Rank', '-'),
            ];
        }

        $myAmount = (float) $selectedRfq->total_freight_charge;

        // $rank = RfqTransporterBid::query()
        //     ->whereRaw(
        //         'CAST(total_freight_charge AS DECIMAL(10,2)) < ?',
        //         [$myAmount]
        //     )
        //     ->selectRaw('CAST(total_freight_charge AS DECIMAL(10,2))')
        //     ->distinct()
        //     ->count() + 1;
        $rank = RfqTransporterBid::query()
            ->where('request_quotation_id', $rfqNumber) // 🔥 MISSING CONDITION
            ->whereRaw(
                'CAST(total_freight_charge AS DECIMAL(10,2)) < ?',
                [$myAmount]
            )
            ->selectRaw('CAST(total_freight_charge AS DECIMAL(10,2))')
            ->distinct()
            ->count() + 1;


            $medal = match (true) {
                $rank == 1 => '🥇',
                $rank == 2 => '🥈',
                $rank == 3 => '🥉',
                default     => '',
            };

        return [
            Stat::make(
                'Total Freight Charge',
                number_format($selectedRfq->total_freight_charge, 2)
            )
                ->description('Transporter: ' . $selectedRfq->transporter_name)
                ->color($rank == 1 ? 'success' : 'primary'),

            Stat::make(
                'Rank',
                trim("{$medal} #{$rank}")
            )
            ->description('Among all transporters')
            ->color(
                $rank == 1 ? 'success' :
                ($rank <= 3 ? 'warning' : 'gray')
            ),
        ];
    }
}
