<?php

declare(strict_types=1);

namespace App\Filament\Resources\Ratings\Tables;

use App\Filament\Support\SelectOptionLabels;

use App\Models\Project;
use App\Models\Rating;
use Filament\Actions\DeleteAction;
use Filament\Actions\EditAction;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;

class RatingsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->modifyQueryUsing(fn (Builder $query): Builder => $query->with(['user']))
            ->columns([
                TextColumn::make('score')->label('امتیاز')->badge()->sortable(),
                TextColumn::make('comment')->label('توضیح')->limit(40)->placeholder('-')->searchable(),
                TextColumn::make('rateable_type')
                    ->label('موضوع')
                    ->badge()
                    ->formatStateUsing(fn ($state) => match (class_basename($state ?? '')) {
                        'Project' => 'پروژه',
                        'Product' => 'محصول',
                        'Post' => 'مقاله',
                        'Service' => 'خدمت',
                        default => class_basename($state ?? '') ?: '-',
                    }),
                TextColumn::make('rateable_id')->label('شناسه')->sortable(),
                TextColumn::make('user.getFilamentName')->label('کاربر')->getStateUsing(fn (Rating $record) => $record->user?->getFilamentName())->searchable(['user.first_name', 'user.last_name', 'user.cellphone', 'user.national_code', 'user.email']),
                TextColumn::make('created_at')->label('تاریخ')->jalaliDateTime('Y/m/d H:i')->sortable(),
            ])
            ->filters([
                SelectFilter::make('rateable_type')
                    ->label('پروژه / محصول')
                    ->options([
                        Project::class => 'پروژه',
                        \App\Models\Product::class => 'محصول',
                    ]),
                SelectFilter::make('user_id')
                    ->label('کاربر')
                    ->relationship('user', 'first_name')
                    ->getOptionLabelFromRecordUsing(fn ($record) => SelectOptionLabels::user($record))
                    ->searchable(['first_name', 'last_name', 'cellphone', 'national_code', 'email']),
                Filter::make('date_range')
                    ->label('تاریخ ثبت')
                    ->schema([
                        DatePicker::make('date_from')->label('از تاریخ')->jalali(),
                        DatePicker::make('date_to')->label('تا تاریخ')->jalali(),
                    ])
                    ->query(function (Builder $q, array $data): Builder {
                        if (filled($data['date_from'] ?? null)) {
                            $q->whereDate('created_at', '>=', $data['date_from']);
                        }
                        if (filled($data['date_to'] ?? null)) {
                            $q->whereDate('created_at', '<=', $data['date_to']);
                        }
                        return $q;
                    }),
            ])
            ->recordActions([
                EditAction::make(),
                DeleteAction::make(),
            ])
            ->defaultSort('created_at', 'desc');
    }
}
