<?php

namespace App\Filament\Construction\Widgets;

use App\Filament\Concerns\InteractsWithDashboardFilters;
use App\Filament\Concerns\PollsEveryThirtySeconds;
use App\Services\Dashboard\ConstructionAnalytics;
use Filament\Widgets\ChartWidget;

class ConstructionExpenseTrendChart extends ChartWidget
{
    use InteractsWithDashboardFilters;
    use PollsEveryThirtySeconds;

    protected static bool $isDiscovered = false;

    protected static ?int $sort = 2;

    protected static ?string $heading = 'Expense Trend';

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

    protected int | string | array $columnSpan = ['default' => 'full', 'lg' => 1];

    protected static string $color = 'warning';

    public static function canView(): bool
    {
        return auth()->user()?->can('cn_view_finance') ?? false;
    }

    public function getDescription(): ?string
    {
        return 'Spending for '.$this->filters()->periodLabel();
    }

    protected function getData(): array
    {
        $chart = ConstructionAnalytics::monthlyExpenses($this->filters());

        return [
            'datasets' => [
                [
                    'label' => 'Expenses (Rs)',
                    'data' => $chart['data'],
                    'borderColor' => '#f97316',
                    'backgroundColor' => 'rgba(249, 115, 22, 0.12)',
                    'fill' => true,
                    'tension' => 0.35,
                ],
            ],
            'labels' => $chart['labels'],
        ];
    }

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