<?php

namespace App\Filament\Widgets;

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

class FunnelStageChart extends ChartWidget
{
    protected static ?string $heading = 'Funnel';

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

    protected static ?int $sort = 2;

    protected static ?string $maxHeight = '260px';

    protected function getData(): array
    {
        $rides = Ride::count();
        $views = Event::where('event_type', 'recommendation_viewed')->count();
        $clicks = Event::where('event_type', 'recommendation_clicked')->count();
        $bookings = Booking::count();

        return [
            'datasets' => [[
                'label' => 'Funnel',
                'data' => [$rides, $views, $clicks, $bookings],
                'backgroundColor' => ['#0f172a', '#f59e0b', '#0f766e', '#16a34a'],
                'borderRadius' => 12,
                'borderSkipped' => false,
            ]],
            'labels' => ['Taxi', 'Espera útil', 'Detalle', 'Reserva'],
        ];
    }

    protected function getOptions(): array
    {
        return [
            'plugins' => [
                'legend' => [
                    'display' => false,
                ],
            ],
            'scales' => [
                'x' => [
                    'grid' => [
                        'display' => false,
                    ],
                ],
                'y' => [
                    'beginAtZero' => true,
                    'grid' => [
                        'color' => 'rgba(148, 163, 184, 0.16)',
                    ],
                    'ticks' => [
                        'precision' => 0,
                    ],
                ],
            ],
        ];
    }

    protected function getType(): string
    {
        return 'bar';
    }
}
