<?php

namespace App\Filament\Central\Widgets;

use App\Models\Central\Subscription;
use Filament\Widgets\ChartWidget;

class RevenueChart extends ChartWidget
{
    protected ?string $heading = 'Venit lunar (MRR)';

    protected static ?int $sort = 2;

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

    protected ?string $pollingInterval = null;

    protected function getData(): array
    {
        $months = collect(range(11, 0))->map(fn ($n) => now()->subMonths($n));

        $values = $months->map(function ($m) {
            return (float) Subscription::where('status', 'paid')
                ->whereYear('paid_at', $m->year)
                ->whereMonth('paid_at', $m->month)
                ->sum('amount');
        });

        return [
            'datasets' => [
                [
                    'label' => 'MRR (MDL)',
                    'data' => $values->toArray(),
                    'backgroundColor' => 'rgba(34, 197, 94, 0.15)',
                    'borderColor' => '#22c55e',
                    'tension' => 0.35,
                    'fill' => true,
                ],
            ],
            'labels' => $months->map(fn ($m) => $m->format('M Y'))->toArray(),
        ];
    }

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