<?php

namespace App\Filament\Widgets;

use App\Enums\AssetStatusEnum;
use App\Models\Asset;
use Filament\Widgets\ChartWidget;

class AssetsByStatusChart extends ChartWidget
{
    protected static ?string $heading = 'Status Aset';

    protected static ?int $sort = 3;

    protected function getData(): array
    {
        $labels = [];
        $data = [];
        $colors = [];

        $colorMap = [
            AssetStatusEnum::AVAILABLE->value => '#10b981',
            AssetStatusEnum::UNAVAILABLE->value => '#ef4444',
            AssetStatusEnum::MAINTENANCE->value => '#f59e0b',
            AssetStatusEnum::INACTIVE->value => '#6b7280',
        ];

        foreach (AssetStatusEnum::cases() as $status) {
            $labels[] = $status->label();
            $data[] = Asset::where('status', $status->value)->count();
            $colors[] = $colorMap[$status->value];
        }

        return [
            'datasets' => [
                [
                    'label' => 'Jumlah Aset',
                    'data' => $data,
                    'backgroundColor' => $colors,
                ],
            ],
            'labels' => $labels,
        ];
    }

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