<?php

declare(strict_types=1);

namespace App\Filament\Resources\Services\Tables;

use App\Filament\Support\SelectOptionLabels;

use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Actions\ForceDeleteBulkAction;
use Filament\Actions\RestoreBulkAction;
use Filament\Tables\Columns\ImageColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ToggleColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;

class ServicesTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                ImageColumn::make('image')
                    ->label('تصویر')
                    ->circular()
                    ->defaultImageUrl(url('/images/placeholder.png')),
                
                TextColumn::make('name')
                    ->label('نام خدمت')
                    ->searchable()
                    ->sortable(),
                
                TextColumn::make('category.title')
                    ->label('دسته')
                    ->searchable()
                    ->sortable(),
                
                TextColumn::make('user.name')
                    ->label('کاربر')
                    ->searchable()
                    ->sortable(),
                
                TextColumn::make('price')
                    ->label('قیمت')
                    ->formatStateUsing(fn ($state) => blank($state) ? null : \Illuminate\Support\Number::format((float) $state) . ' تومان')
                    ->sortable(),
                
                TextColumn::make('duration')
                    ->label('مدت زمان')
                    ->sortable(),
                
                TextColumn::make('status')
                    ->label('وضعیت')
                    ->badge()
                    ->color(fn (string $state): string => match ($state) {
                        'active' => 'success',
                        'inactive' => 'danger',
                        'pending' => 'warning',
                        default => 'gray',
                    })
                    ->formatStateUsing(fn (string $state): string => match ($state) {
                        'active' => 'فعال',
                        'inactive' => 'غیرفعال',
                        'pending' => 'در انتظار',
                        default => $state,
                    }),
                
                ToggleColumn::make('is_active')
                    ->label('فعال'),
                
                TextColumn::make('view_count')
                    ->label('بازدید')
                    ->sortable(),
                
                TextColumn::make('createdBy.name')
                    ->label('ایجادکننده')
                    ->hidden()
                    ->searchable()
                    ->sortable(),
                
                TextColumn::make('created_at')
                    ->label('تاریخ ایجاد')
                    ->jalaliDateTime('Y/m/d H:i')
                    ->sortable(),
            ])
            ->filters([
                TrashedFilter::make(),
                SelectFilter::make('status')
                    ->label('وضعیت')
                    ->options([
                        'active' => 'فعال',
                        'inactive' => 'غیرفعال',
                        'pending' => 'در انتظار',
                    ]),
                SelectFilter::make('is_active')
                    ->label('فعال')
                    ->options([
                        1 => 'بله',
                        0 => 'خیر',
                    ]),
                SelectFilter::make('category_id')
                    ->label('دسته')
                    ->relationship('category', 'title')
                    ->getOptionLabelFromRecordUsing(fn ($record) => SelectOptionLabels::category($record))
                    ->searchable()
                    ->preload(),
                SelectFilter::make('user_id')
                    ->label('کاربر')
                    ->relationship('user', 'name')
                    ->searchable(['name', 'cellphone'])
                    ->getOptionLabelFromRecordUsing(fn ($record) => SelectOptionLabels::user($record))
                    ->preload(),
                SelectFilter::make('province_id')
                    ->label('استان')
                    ->relationship('province', 'name')
                    ->getOptionLabelFromRecordUsing(fn ($record) => SelectOptionLabels::generic($record, 'name', 'استان'))
                    ->searchable()
                    ->preload(),
                SelectFilter::make('county_id')
                    ->label('شهرستان')
                    ->relationship('county', 'name')
                    ->getOptionLabelFromRecordUsing(fn ($record) => SelectOptionLabels::generic($record, 'name', 'شهر'))
                    ->searchable()
                    ->preload(),
            ])
            ->recordActions([
                EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                    ForceDeleteBulkAction::make(),
                    RestoreBulkAction::make(),
                ]),
            ]);
    }
}

