<?php

namespace App\Filament\Widgets;

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

class SourceChannelChart extends ChartWidget
{
    protected static ?string $heading = 'Taxi requests por canal';

    protected function getData(): array
    {
        $channels = Ride::query()
            ->selectRaw('source_channel, COUNT(*) as aggregate')
            ->groupBy('source_channel')
            ->orderByDesc('aggregate')
            ->pluck('aggregate', 'source_channel');

        return [
            'datasets' => [[
                'label' => 'Solicitudes',
                'data' => $channels->values()->all(),
                'backgroundColor' => ['#0f766e', '#14b8a6', '#f59e0b', '#0f172a'],
                'borderWidth' => 0,
            ]],
            'labels' => $channels->keys()->map(fn (string $channel) => ucfirst($channel))->all(),
        ];
    }

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