<?php

namespace App\Filament\Resources\Users\Tables;

use App\Services\RoleAssignmentService;
use Filament\Actions\ActionGroup;
use Filament\Actions\DeleteAction;
use Filament\Actions\EditAction;
use Filament\Notifications\Notification;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TernaryFilter;
use Filament\Tables\Table;
use Illuminate\Validation\ValidationException;

class UsersTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('name')->label(__('roles.fields.name'))->searchable()->sortable(),
                TextColumn::make('email')->label(__('roles.fields.email'))->searchable()->sortable(),
                TextColumn::make('roles.name')->label(__('roles.fields.roles'))->badge()->formatStateUsing(fn ($state) => __('roles.names.'.$state))->wrap(),
                TextColumn::make('status')->label(__('roles.fields.status'))->badge()->formatStateUsing(fn ($state) => $state->label()),
                TextColumn::make('email_verified_at')->label(__('roles.fields.verified'))->dateTime('d.m.Y H:i')->placeholder(__('roles.not_verified'))->sortable(),
                TextColumn::make('last_login_at')->label(__('roles.fields.last_login'))->dateTime('d.m.Y H:i')->placeholder(__('roles.never'))->sortable(),
                TextColumn::make('created_at')->label(__('roles.fields.created_at'))->dateTime('d.m.Y H:i')->sortable(),
            ])
            ->filters([
                SelectFilter::make('role')->label(__('roles.fields.roles'))->relationship('roles', 'name')->multiple()
                    ->getOptionLabelFromRecordUsing(fn ($record) => __('roles.names.'.$record->name)),
                SelectFilter::make('status')->label(__('roles.fields.status'))->options(['active' => __('states.active'), 'suspended' => __('states.suspended')]),
                TernaryFilter::make('verified')->label(__('roles.fields.verified'))
                    ->queries(true: fn ($query) => $query->whereNotNull('email_verified_at'), false: fn ($query) => $query->whereNull('email_verified_at')),
            ])
            ->recordActions(ActionGroup::make([
                EditAction::make()->label(__('roles.edit_user')),
                DeleteAction::make()
                    ->label(__('roles.delete'))
                    ->authorize('delete')
                    ->modalHeading(__('roles.delete_confirmation'))
                    ->modalDescription(fn ($record) => $record->name.' - '.$record->email)
                    ->modalSubmitActionLabel(__('roles.delete_user'))
                    ->modalCancelActionLabel(__('roles.cancel'))
                    ->successNotificationTitle(__('roles.deleted'))
                    ->action(function ($record): void {
                        try {
                            app(RoleAssignmentService::class)->delete(auth()->user(), $record);
                            Notification::make()->success()->title(__('roles.deleted'))->send();
                        } catch (ValidationException $exception) {
                            Notification::make()->danger()->title(collect($exception->errors())->flatten()->first() ?? __('roles.delete_failed'))->send();
                        }
                    }),
            ]))
            ->toolbarActions([]);
    }
}
