<?php
/*
    * Created by VahabDev in Aparat Live
    * Website: https://pourfi.ir
    * Aparat Live: https://www.aparat.com/vahabdev/live
    * Aparat Channel: https://www.aparat.com/vahabdev
    * Rubika Channel: https://rubika.ir/vahabdev
    * Donate: https://reymit.ir/vahabdev
    * Telegram: https://t.me/vahabdev
*/


namespace App\Filament\Resources\Tickets\Tables;

use App\Models\Ticket;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Actions\ViewAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;

class TicketsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->modifyQueryUsing(function (Builder $query) {
                if ($query->getModel() instanceof Ticket) {
                    $query->orderByRaw("
                    CASE
                        WHEN status = 'open' THEN 1
                        WHEN status = 'user_replay' THEN 2
                        ELSE 3
                    END ASC
                ")->orderBy('id', 'desc');
                }
            })
            ->columns([
                TextColumn::make('ticket_number')
                    ->label('شماره')
                    ->searchable(),
                TextColumn::make('user.firstname')
                    ->label('کاربر')
                    ->description(fn ($record) => $record->user?->phone) // شماره تلفن را در خط دوم نمایش می‌دهد
                    ->searchable(['firstname', 'phone']),
                TextColumn::make('subject')->label('موضوع')->limit(30),
                TextColumn::make('status')
                    ->label('وضعیت')
                    ->badge()
                    ->formatStateUsing(fn (Ticket $record) => $record->status_label)
                    ->color(fn (string $state): string => match ($state) {
                        'open' => 'info',
                        'user_replay' => 'warning',
                        'admin_replay' => 'success',
                        'closed' => 'gray',
                        default => 'primary',
                    }),
                TextColumn::make('priority')
                    ->label('اولویت')
                    ->formatStateUsing(fn (Ticket $record) => $record->priority_label),
                TextColumn::make('created_at')->label('تاریخ')->dateTime('Y/m/d H:i')->sortable(),
            ])
            ->filters([
                SelectFilter::make('status')->label('فیلتر وضعیت')->options([
                    'open' => 'باز',
                    'user_replay' => 'پاسخ داده شده توسط کاربر',
                    'admin_replay' => 'پاسخ داده شده توسط ادمین',
                    'closed' => 'بسته شده',
                ]),
            ])
            ->recordActions([
                ViewAction::make(),
                EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }
}
