<?php

namespace App\Filament\Widgets;

use App\Models\Booking;
use App\Models\Event;
use App\Models\Ride;
use Filament\Widgets\StatsOverviewWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;

class FunnelOverview extends StatsOverviewWidget
{
    protected function getStats(): array
    {
        $rides = Ride::count();
        $views = Event::where('event_type', 'recommendation_viewed')->count();
        $clicks = Event::where('event_type', 'recommendation_clicked')->count();
        $bookings = Booking::count();
        $gmv = (float) Booking::sum('amount');
        $commission = (float) Booking::sum('commission_amount');

        $viewToClick = $views > 0 ? round(($clicks / $views) * 100, 1) : 0;
        $rideToBooking = $rides > 0 ? round(($bookings / $rides) * 100, 1) : 0;

        return [
            Stat::make('Taxi requests', number_format($rides))
                ->description('Entradas del funnel')
                ->color('primary'),
            Stat::make('Recommendation views', number_format($views))
                ->description('Usuarios expuestos a propuestas')
                ->color('info'),
            Stat::make('Clicks', number_format($clicks))
                ->description('CTR vista → clic: '.$viewToClick.'%')
                ->color('warning'),
            Stat::make('Bookings', number_format($bookings))
                ->description('Conversión ride → booking: '.$rideToBooking.'% · GMV €'.number_format($gmv, 0))
                ->color('success'),
            Stat::make('Comisión estimada', '€'.number_format($commission, 0))
                ->description('Valor económico demo ya atribuible')
                ->color('primary'),
        ];
    }
}
