<?php

declare(strict_types=1);

namespace App\Filament\Resources\Employees\RelationManagers;

use App\Enums\ApprovalStatus;
use App\Filament\Support\HrForm;
use App\Models\UnpaidLeave;
use App\Services\Leave\UnpaidLeaveService;
use Filament\Actions\Action;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\CreateAction;
use Filament\Actions\DeleteAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Actions\ForceDeleteBulkAction;
use Filament\Actions\RestoreBulkAction;
use Filament\Forms\Components\Textarea;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;
use Illuminate\Support\Facades\Auth;

class UnpaidLeavesRelationManager extends RelationManager
{
    protected static string $relationship = 'unpaidLeaves';

    public static function getTitle(\Illuminate\Database\Eloquent\Model $ownerRecord, string $pageClass): string
    {
        return __("hr.relation_managers.unpaid_leaves");
    }

    public function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                HrForm::leaveDatePicker('start_date', __('hr.fields.start_date')),
                HrForm::leaveDatePicker('end_date', __('hr.fields.end_date')),
                HrForm::leaveDaysDisplay(),
                Textarea::make('reason')
                    ->label(__('hr.fields.reason'))
                    ->rows(3)
                    ->columnSpanFull(),
            ]);
    }

    public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('start_date')
            ->columns([
                TextColumn::make('start_date')
                    ->label(__('hr.fields.start_date'))
                    ->date()
                    ->sortable(),
                TextColumn::make('end_date')
                    ->label(__('hr.fields.end_date'))
                    ->date()
                    ->sortable(),
                TextColumn::make('days')
                    ->label(__('hr.fields.days'))
                    ->numeric()
                    ->sortable(),
                TextColumn::make('status')
                    ->label(__('hr.fields.status'))
                    ->badge()
                    ->color(fn (ApprovalStatus $state): string => $state->color())
                    ->formatStateUsing(fn (ApprovalStatus $state): string => $state->label())
                    ->sortable(),
                TextColumn::make('approver.name')
                    ->label(__('hr.fields.approved_by'))
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                SelectFilter::make('status')
                    ->label(__('hr.fields.status'))
                    ->options(ApprovalStatus::class),
                TrashedFilter::make(),
            ])
            ->headerActions([
                CreateAction::make(),
            ])
            ->recordActions([
                Action::make('approve')
                    ->label(__('hr.actions.approve'))
                    ->icon('heroicon-o-check-badge')
                    ->color('success')
                    ->requiresConfirmation()
                    ->visible(fn (UnpaidLeave $record): bool => $record->status === ApprovalStatus::Pending)
                    ->action(function (UnpaidLeave $record, UnpaidLeaveService $unpaidLeaveService): void {
                        $unpaidLeaveService->approve($record, Auth::user());
                    }),
                Action::make('reject')
                    ->label(__('hr.actions.reject'))
                    ->icon('heroicon-o-x-mark')
                    ->color('danger')
                    ->requiresConfirmation()
                    ->visible(fn (UnpaidLeave $record): bool => $record->status === ApprovalStatus::Pending)
                    ->action(function (UnpaidLeave $record, UnpaidLeaveService $unpaidLeaveService): void {
                        $unpaidLeaveService->reject($record, Auth::user());
                    }),
                EditAction::make(),
                DeleteAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                    ForceDeleteBulkAction::make(),
                    RestoreBulkAction::make(),
                ]),
            ]);
    }
}
