<?php

declare(strict_types=1);

namespace App\Filament\Resources\ActivityLogs\Tables;

use App\Models\Bonus;
use App\Models\Department;
use App\Models\DisciplinaryReport;
use App\Models\Employee;
use App\Models\User;
use App\Models\Vacation;
use Filament\Actions\ViewAction;
use Filament\Forms\Components\DatePicker;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Spatie\Activitylog\Models\Activity;

class ActivityLogsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('created_at')
                    ->label(__('hr.fields.date'))
                    ->dateTime()
                    ->sortable(),
                TextColumn::make('description')
                    ->label(__('hr.fields.description'))
                    ->searchable()
                    ->limit(50),
                TextColumn::make('event')
                    ->label(__('hr.fields.event'))
                    ->badge()
                    ->searchable()
                    ->sortable(),
                TextColumn::make('subject_type')
                    ->label(__('hr.fields.subject_type'))
                    ->formatStateUsing(fn (?string $state): string => $state ? class_basename($state) : '—')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('subject_id')
                    ->label(__('hr.fields.subject_id'))
                    ->sortable(),
                TextColumn::make('causer.name')
                    ->label(__('hr.fields.causer'))
                    ->searchable()
                    ->sortable(),
                TextColumn::make('log_name')
                    ->label(__('hr.fields.log_name'))
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->defaultSort('created_at', 'desc')
            ->filters([
                SelectFilter::make('subject_type')
                    ->label(__('hr.fields.subject_type'))
                    ->options([
                        Employee::class => __('hr.audit.employee'),
                        Vacation::class => __('hr.audit.vacation'),
                        Bonus::class => __('hr.audit.bonus'),
                        DisciplinaryReport::class => __('hr.audit.disciplinary_report'),
                        Department::class => __('hr.audit.department'),
                        User::class => __('hr.audit.user'),
                    ]),
                SelectFilter::make('event')
                    ->label(__('hr.fields.event'))
                    ->options(fn (): array => Activity::query()
                        ->whereNotNull('event')
                        ->distinct()
                        ->pluck('event', 'event')
                        ->all()),
                SelectFilter::make('causer_id')
                    ->label(__('hr.fields.causer'))
                    ->options(fn (): array => User::query()
                        ->whereIn('id', Activity::query()
                            ->where('causer_type', User::class)
                            ->whereNotNull('causer_id')
                            ->distinct()
                            ->pluck('causer_id'))
                        ->orderBy('name')
                        ->pluck('name', 'id')
                        ->all())
                    ->searchable()
                    ->query(fn (Builder $query, array $data): Builder => $query->when(
                        $data['value'] ?? null,
                        fn (Builder $query, $causerId): Builder => $query
                            ->where('causer_id', $causerId)
                            ->where('causer_type', User::class),
                    )),
                Filter::make('created_at')
                    ->label(__('hr.fields.date'))
                    ->schema([
                        DatePicker::make('from')
                            ->label(__('hr.fields.from')),
                        DatePicker::make('until')
                            ->label(__('hr.fields.until')),
                    ])
                    ->query(function (Builder $query, array $data): Builder {
                        return $query
                            ->when(
                                $data['from'] ?? null,
                                fn (Builder $query, string $date): Builder => $query->whereDate('created_at', '>=', $date),
                            )
                            ->when(
                                $data['until'] ?? null,
                                fn (Builder $query, string $date): Builder => $query->whereDate('created_at', '<=', $date),
                            );
                    }),
            ])
            ->recordActions([
                ViewAction::make(),
            ])
            ->toolbarActions([]);
    }
}
