<?php

declare(strict_types=1);

namespace App\Filament\Resources\Users\Tables;

use App\Filament\Support\SelectOptionLabels;

use App\Models\User;
use Filament\Actions\Action;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Actions\ForceDeleteBulkAction;
use Filament\Actions\RestoreBulkAction;
use Filament\Forms\Components\Textarea;
use Filament\Notifications\Notification;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;

class UsersTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                \App\Filament\Components\Table\FirstNameTextColumn::create(),
                \App\Filament\Components\Table\LastNameTextColumn::create(),
                \App\Filament\Components\Table\CellphoneTextColumn::create(),
                \App\Filament\Components\Table\EmailTextColumn::create(),
                TextColumn::make('roles.name')
                    ->label('نقش')
                    ->badge()
                    ->separator(',')
                    ->formatStateUsing(fn ($state) => User::roleOptions()[$state] ?? $state),
                TextColumn::make('account_type')
                    ->label('نوع حساب')
                    ->badge()
                    ->formatStateUsing(fn (?string $state) => User::accountTypeOptions()[$state] ?? $state ?? '-'),
                IconColumn::make('is_blocked')->label('مسدود')->boolean(),
                TextColumn::make('status')
                    ->label('وضعیت')
                    ->badge()
                    ->formatStateUsing(fn ($state) => ((int) $state === 1) ? 'فعال' : 'غیرفعال')
                    ->color(fn ($state) => ((int) $state === 1) ? 'success' : 'danger'),
                \App\Filament\Components\Table\CreatedAtTextColumn::create(),
            ])
            ->filters([
                TrashedFilter::make(),
                SelectFilter::make('status')
                    ->label('وضعیت')
                    ->options([
                        1 => 'فعال',
                        0 => 'غیرفعال',
                    ]),
                SelectFilter::make('account_type')
                    ->label('نوع حساب')
                    ->options(User::accountTypeOptions()),
                SelectFilter::make('roles')
                    ->label('نقش')
                    ->relationship('roles', 'name')
                    ->getOptionLabelFromRecordUsing(fn ($record) => SelectOptionLabels::generic($record, 'name', 'نقش')),
                SelectFilter::make('is_blocked')
                    ->label('مسدود')
                    ->options([
                        1 => 'بله',
                        0 => 'خیر',
                    ]),
            ])
            ->defaultSort('created_at', 'desc')
            ->recordActions([
                EditAction::make(),
                Action::make('block')
                    ->label('مسدود')
                    ->icon('heroicon-o-no-symbol')
                    ->color('danger')
                    ->visible(fn (User $u) => ! $u->is_blocked)
                    ->form([Textarea::make('blocked_reason')->label('دلیل')->required()->rows(3)])
                    ->action(function (User $record, array $data): void {
                        $record->update([
                            'is_blocked' => true,
                            'status' => 0,
                            'blocked_at' => now(),
                            'blocked_reason' => $data['blocked_reason'],
                            'status_desc' => $data['blocked_reason'],
                        ]);
                        $record->tokens()->delete();
                        Notification::make()->title('کاربر مسدود شد.')->warning()->send();
                    }),
                Action::make('unblock')
                    ->label('رفع مسدودی')
                    ->icon('heroicon-o-check')
                    ->color('success')
                    ->visible(fn (User $u) => (bool) $u->is_blocked)
                    ->action(function (User $record): void {
                        $record->update([
                            'is_blocked' => false,
                            'status' => 1,
                            'blocked_at' => null,
                            'blocked_reason' => null,
                            'status_desc' => null,
                        ]);
                        Notification::make()->title('مسدودیت برداشته شد.')->success()->send();
                    }),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                    ForceDeleteBulkAction::make(),
                    RestoreBulkAction::make(),
                ]),
            ]);
    }
}
