<?php

declare(strict_types=1);

namespace App\Filament\Resources\UserDevices\Tables;

use App\Models\UserDevice;
use Filament\Actions\Action;
use Filament\Actions\DeleteAction;
use Filament\Notifications\Notification;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TernaryFilter;
use Filament\Tables\Table;

class UserDevicesTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->modifyQueryUsing(fn ($query) => $query->with('user'))
            ->defaultSort('last_used_at', 'desc')
            ->columns([
                TextColumn::make('id')->label('#')->sortable(),
                TextColumn::make('user.getFilamentName')
                    ->label('کاربر')
                    ->getStateUsing(fn (UserDevice $record) => $record->user?->getFilamentName() ?? '-')
                    ->searchable(query: function ($query, string $search) {
                        $query->whereHas('user', function ($q) use ($search): void {
                            $q->where('first_name', 'like', "%{$search}%")
                                ->orWhere('last_name', 'like', "%{$search}%")
                                ->orWhere('cellphone', 'like', "%{$search}%");
                        });
                    }),
                TextColumn::make('name')->label('نام دستگاه')->limit(40)->placeholder('-'),
                TextColumn::make('ip')->label('IP')->placeholder('-'),
                IconColumn::make('is_current')->label('فعلی')->boolean(),
                IconColumn::make('is_blocked')->label('مسدود')->boolean(),
                TextColumn::make('last_used_at')->label('آخرین استفاده')->jalaliDateTime('Y/m/d H:i')->sortable()->placeholder('-'),
                TextColumn::make('created_at')->label('ثبت')->jalaliDateTime('Y/m/d H:i')->sortable()->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                TernaryFilter::make('is_blocked')->label('مسدود'),
                TernaryFilter::make('is_current')->label('دستگاه فعلی'),
                SelectFilter::make('user_id')
                    ->label('کاربر')
                    ->relationship('user', 'first_name')
                    ->searchable()
                    ->preload(),
            ])
            ->recordActions([
                Action::make('block')
                    ->label('مسدود')
                    ->icon('heroicon-o-no-symbol')
                    ->color('danger')
                    ->visible(fn (UserDevice $record) => ! $record->is_blocked)
                    ->requiresConfirmation()
                    ->action(function (UserDevice $record): void {
                        $record->update(['is_blocked' => true]);
                        Notification::make()->title('دستگاه مسدود شد.')->warning()->send();
                    }),
                Action::make('unblock')
                    ->label('رفع مسدودیت')
                    ->icon('heroicon-o-check')
                    ->color('success')
                    ->visible(fn (UserDevice $record) => $record->is_blocked)
                    ->action(function (UserDevice $record): void {
                        $record->update(['is_blocked' => false]);
                        Notification::make()->title('مسدودیت برداشته شد.')->success()->send();
                    }),
                DeleteAction::make(),
            ]);
    }
}
