<?php

namespace App\Filament\Widgets;

use App\Models\Booking;
use App\Models\Event;
use App\Models\Ride;
use Filament\Widgets\Widget;

class JourneyStoryboard extends Widget
{
    protected static string $view = 'filament.widgets.journey-storyboard';

    protected int | string | array $columnSpan = 'full';

    protected static ?int $sort = 1;

    protected function getViewData(): array
    {
        $rides = Ride::count();
        $views = Event::where('event_type', 'recommendation_viewed')->count();
        $clicks = Event::where('event_type', 'recommendation_clicked')->count();
        $bookingsCount = Booking::count();
        $commission = (float) Booking::sum('commission_amount');
        $gmv = (float) Booking::sum('amount');
        $rideToBooking = $rides > 0 ? round(($bookingsCount / $rides) * 100, 1) : 0;
        $clickThrough = $views > 0 ? round(($clicks / $views) * 100, 1) : 0;

        return [
            'rides' => $rides,
            'views' => $views,
            'clicks' => $clicks,
            'bookingsCount' => $bookingsCount,
            'commission' => $commission,
            'gmv' => $gmv,
            'rideToBooking' => $rideToBooking,
            'clickThrough' => $clickThrough,
        ];
    }
}
