<?php

declare(strict_types=1);

namespace App\Filament\Widgets;

use App\Enums\ApprovalStatus;
use App\Models\Vacation;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget;
use Illuminate\Database\Eloquent\Builder;

class UpcomingLeaveWidget extends TableWidget
{
    protected int | string | array $columnSpan = 1;

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

    public function table(Table $table): Table
    {
        return $table
            ->query(
                Vacation::query()
                    ->with('employee')
                    ->where('status', ApprovalStatus::Approved)
                    ->whereDate('start_date', '>=', now()->toDateString())
                    ->whereDate('start_date', '<=', now()->addDays(14)->toDateString())
                    ->orderBy('start_date'),
            )
            ->columns([
                TextColumn::make('employee.full_name')
                    ->label(__('hr.fields.employee')),
                TextColumn::make('start_date')
                    ->date(),
                TextColumn::make('end_date')
                    ->date(),
                TextColumn::make('days')
                    ->label(__('hr.fields.days')),
            ])
            ->paginated(false);
    }

    /**
     * @return Builder<Vacation>|null
     */
    protected function getTableQuery(): ?Builder
    {
        return null;
    }
}
