<?php

declare(strict_types=1);

namespace App\Filament\Widgets;

use App\Enums\ApprovalStatus;
use App\Models\Vacation;
use Filament\Widgets\ChartWidget;

class MonthlyLeaveChart extends ChartWidget
{
    protected int | string | array $columnSpan = 1;

    public function getHeading(): ?string
    {
        return __('hr.widgets.vacation_days_by_month');
    }

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

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

        for ($i = 11; $i >= 0; $i--) {
            $month = now()->subMonths($i);
            $labels[] = $month->format('M Y');
            $data[] = (int) Vacation::query()
                ->where('status', ApprovalStatus::Approved)
                ->whereYear('start_date', $month->year)
                ->whereMonth('start_date', $month->month)
                ->sum('days');
        }

        return [
            'datasets' => [
                [
                    'label' => __('hr.widgets.vacation_days'),
                    'data' => $data,
                    'backgroundColor' => '#f59e0b',
                ],
            ],
            'labels' => $labels,
        ];
    }
}
