<?php

namespace App\Filament\RealEstate\Widgets;

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

class RealEstatePropertyStatusChart extends ChartWidget
{
    use InteractsWithDashboardFilters;
    use PollsEveryThirtySeconds;

    protected static bool $isDiscovered = false;

    protected static ?int $sort = 3;

    protected static ?string $heading = 'Property Inventory';

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

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

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

    public function getDescription(): ?string
    {
        return 'Filtered inventory breakdown';
    }

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

        return [
            'datasets' => [
                [
                    'data' => $chart['data'],
                    'backgroundColor' => ['#10b981', '#6366f1', '#f59e0b'],
                    'borderWidth' => 0,
                ],
            ],
            'labels' => $chart['labels'],
        ];
    }

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