<?php

declare(strict_types=1);

namespace App\Filament\Widgets;

use App\Models\Employee;
use Filament\Widgets\ChartWidget;

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

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

    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[] = Employee::query()
                ->whereNotNull('hire_date')
                ->whereYear('hire_date', $month->year)
                ->whereMonth('hire_date', $month->month)
                ->count();
        }

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