<?php

declare(strict_types=1);

namespace App\Filament\Resources\Experts\Tables;

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

class ExpertsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('display_name')
                    ->label('نام خدمات‌دهنده')
                    ->searchable(['brand_name', 'user.first_name', 'user.last_name'])
                    ->sortable(),
                TextColumn::make('main_category')
                    ->label('حوزه فعالیت')
                    ->searchable()
                    ->placeholder('-'),
                TextColumn::make('workplace')
                    ->label('محل فعالیت')
                    ->placeholder('-')
                    ->toggleable(),
                TextColumn::make('work_phone')
                    ->label('شماره تماس')
                    ->placeholder('-'),
                TextColumn::make('work_email')
                    ->label('ایمیل')
                    ->placeholder('-')
                    ->toggleable(),
                TextColumn::make('status')
                    ->label('وضعیت حساب')
                    ->badge()
                    ->formatStateUsing(fn (?string $state) => Expert::statusOptions()[$state] ?? $state ?? '-')
                    ->color(fn (?string $state) => match ($state) {
                        Expert::STATUS_APPROVED, 'active' => 'success',
                        Expert::STATUS_PENDING => 'warning',
                        Expert::STATUS_NEEDS_EDIT => 'info',
                        Expert::STATUS_REJECTED, Expert::STATUS_BLOCKED => 'danger',
                        default => 'gray',
                    })
                    ->sortable(),
                TextColumn::make('admin_reason')->label('دلیل ادمین')->limit(30)->toggleable()->toggledHiddenByDefault(),
                TextColumn::make('created_at')
                    ->label('تاریخ ثبت')
                    ->jalaliDate('Y/m/d')
                    ->sortable(),
            ])
            ->filters([
                TrashedFilter::make(),
                SelectFilter::make('status')
                    ->label('وضعیت')
                    ->options(Expert::statusOptions()),
            ])
            ->recordActions([
                ViewAction::make()->label('مشاهده کارنامه'),
                Action::make('approve')
                    ->label('تایید حساب')
                    ->icon('heroicon-o-check')
                    ->color('success')
                    ->visible(fn (Expert $e) => $e->status !== Expert::STATUS_APPROVED)
                    ->action(function (Expert $record): void {
                        $record->update([
                            'status' => Expert::STATUS_APPROVED,
                            'admin_reason' => null,
                            'reviewed_by' => auth()->id(),
                            'reviewed_at' => now(),
                        ]);
                        $record->user?->syncRoles([\App\Models\User::ROLE_EXPERT, \App\Models\User::ROLE_USER]);
                        $record->user?->update(['account_type' => \App\Models\User::ACCOUNT_EXPERT]);
                        Notification::make()->title('حساب تایید شد.')->success()->send();
                    }),
                Action::make('reject')
                    ->label('رد')
                    ->icon('heroicon-o-x-mark')
                    ->color('danger')
                    ->visible(fn (Expert $e) => $e->status === Expert::STATUS_PENDING)
                    ->form([Textarea::make('admin_reason')->label('دلیل رد')->required()->rows(3)])
                    ->action(function (Expert $record, array $data): void {
                        $record->update([
                            'status' => Expert::STATUS_REJECTED,
                            'admin_reason' => $data['admin_reason'],
                            'reviewed_by' => auth()->id(),
                            'reviewed_at' => now(),
                        ]);
                        Notification::make()->title('حساب رد شد.')->warning()->send();
                    }),
                Action::make('needs_edit')
                    ->label('نیاز به ویرایش')
                    ->icon('heroicon-o-pencil-square')
                    ->color('warning')
                    ->visible(fn (Expert $e) => $e->status === Expert::STATUS_PENDING)
                    ->form([Textarea::make('admin_reason')->label('موارد اصلاح')->required()->rows(3)])
                    ->action(function (Expert $record, array $data): void {
                        $record->update([
                            'status' => Expert::STATUS_NEEDS_EDIT,
                            'admin_reason' => $data['admin_reason'],
                            'reviewed_by' => auth()->id(),
                            'reviewed_at' => now(),
                        ]);
                        Notification::make()->title('برای ویرایش ارجاع شد.')->warning()->send();
                    }),
                Action::make('block')
                    ->label('مسدود')
                    ->icon('heroicon-o-lock-closed')
                    ->color('danger')
                    ->visible(fn (Expert $e) => $e->status !== Expert::STATUS_BLOCKED)
                    ->action(function (Expert $record): void {
                        $record->update(['status' => Expert::STATUS_BLOCKED]);
                        Notification::make()->title('حساب مسدود شد.')->danger()->send();
                    }),
                EditAction::make()->label('ویرایش اطلاعات'),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                    ForceDeleteBulkAction::make(),
                    RestoreBulkAction::make(),
                ]),
            ]);
    }
}
