<?php

declare(strict_types=1);

namespace App\Filament\Resources\Employees\RelationManagers;

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

class GiftsRelationManager extends RelationManager
{
    protected static string $relationship = 'gifts';

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

    public function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                DatePicker::make('gift_date')
                    ->label(__('hr.fields.gift_date'))
                    ->required()
                    ->default(now()),
                TextInput::make('gift_name')
                    ->label(__('hr.fields.gift_name'))
                    ->required()
                    ->maxLength(255),
                TextInput::make('value')
                    ->label(__('hr.fields.value'))
                    ->required()
                    ->numeric()
                    ->prefix('TMT')
                    ->minValue(0)
                    ->step(0.01)
                    ->default(0),
                Textarea::make('reason')
                    ->label(__('hr.fields.reason'))
                    ->rows(3)
                    ->columnSpanFull(),
            ]);
    }

    public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('gift_name')
            ->columns([
                TextColumn::make('gift_date')
                    ->label(__('hr.fields.gift_date'))
                    ->date()
                    ->sortable(),
                TextColumn::make('gift_name')
                    ->label(__('hr.fields.gift'))
                    ->searchable()
                    ->sortable(),
                TextColumn::make('value')
                    ->label(__('hr.fields.value'))
                    ->money('TMT')
                    ->sortable(),
                TextColumn::make('reason')
                    ->label(__('hr.fields.reason'))
                    ->limit(40)
                    ->toggleable(),
            ])
            ->filters([
                TrashedFilter::make(),
            ])
            ->headerActions([
                CreateAction::make(),
            ])
            ->recordActions([
                EditAction::make(),
                DeleteAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                    ForceDeleteBulkAction::make(),
                    RestoreBulkAction::make(),
                ]),
            ]);
    }
}
