<?php

declare(strict_types=1);

namespace App\Filament\Resources\Employees\RelationManagers;

use App\Filament\Support\HrForm;
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\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;

class SickLeavesRelationManager extends RelationManager
{
    protected static string $relationship = 'sickLeaves';

    public static function getTitle(\Illuminate\Database\Eloquent\Model $ownerRecord, string $pageClass): string
    {
        return __("hr.relation_managers.sick_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(),
                TextInput::make('medical_institution')
                    ->label(__('hr.fields.institution'))
                    ->maxLength(255),
                Textarea::make('reason')
                    ->label(__('hr.fields.reason'))
                    ->rows(3)
                    ->columnSpanFull(),
                HrForm::fileUpload('document_path')
                    ->label(__('hr.fields.medical_document'))
                    ->acceptedFileTypes(['application/pdf', 'image/*'])
                    ->maxSize(10240),
            ]);
    }

    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('medical_institution')
                    ->label(__('hr.fields.institution'))
                    ->searchable()
                    ->toggleable(),
                IconColumn::make('document_path')
                    ->label(__('hr.fields.document'))
                    ->boolean()
                    ->trueIcon('heroicon-o-document-check')
                    ->falseIcon('heroicon-o-document'),
            ])
            ->filters([
                TrashedFilter::make(),
            ])
            ->headerActions([
                CreateAction::make(),
            ])
            ->recordActions([
                EditAction::make(),
                DeleteAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                    ForceDeleteBulkAction::make(),
                    RestoreBulkAction::make(),
                ]),
            ]);
    }
}
