<?php

declare(strict_types=1);

namespace App\Filament\Widgets;

use App\Models\Order;
use Filament\Widgets\ChartWidget;
use Flowframe\Trend\Trend;
use Flowframe\Trend\TrendValue;

class OrdersRevenueChart extends ChartWidget
{
    protected ?string $heading = 'درآمد سفارشات (ماهانه)';

    protected static ?int $sort = 5;

    protected function getData(): array
    {
        $data = Trend::query(Order::withoutGlobalScopes()->query())
            ->between(
                start: now()->startOfYear(),
                end: now()->endOfYear(),
            )
            ->perMonth()
            ->sum('total_price');

        return [
            'datasets' => [
                [
                    'label' => 'درآمد (تومان)',
                    'data' => $data->map(fn (TrendValue $value) => (int) $value->aggregate),
                    'backgroundColor' => 'rgba(168, 85, 247, 0.5)',
                    'borderColor' => 'rgb(168, 85, 247)',
                ],
            ],
            'labels' => $data->map(fn (TrendValue $value) => $value->date),
        ];
    }

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