<?php

declare(strict_types=1);

namespace App\Filament\Resources\AdminMessages\Tables;

use App\Filament\Support\SelectOptionLabels;

use App\Models\AdminMessage;
use Filament\Forms\Components\DatePicker;
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 AdminMessagesTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('title')->label('عنوان')->limit(40)->placeholder('-')->searchable(),
                TextColumn::make('body')->label('متن')->limit(50)->placeholder('-')->wrap(),
                TextColumn::make('status')->label('وضعیت')->badge()->formatStateUsing(fn (string $state) => $state === AdminMessage::STATUS_DRAFT ? 'پیش‌نویس' : 'ارسال شده')->color(fn (string $state) => $state === AdminMessage::STATUS_DRAFT ? 'gray' : 'success'),
                TextColumn::make('channel')->label('مدل ارسال')->formatStateUsing(fn ($state) => match ($state) {
                    AdminMessage::CHANNEL_SMS => 'پیامک',
                    AdminMessage::CHANNEL_EMAIL => 'ایمیل',
                    AdminMessage::CHANNEL_INTERNAL => 'داخلی',
                    default => $state ?? '-',
                }),
                TextColumn::make('project.name')->label('پروژه')->placeholder('-')->toggleable(),
                TextColumn::make('recipients_count')->label('گیرندگان')->counts('recipients'),
                TextColumn::make('sent_at')->label('زمان ارسال')->jalaliDateTime('Y/m/d H:i')->placeholder('-'),
                TextColumn::make('created_at')->label('تاریخ')->jalaliDateTime('Y/m/d H:i')->sortable(),
            ])
            ->filters([
                SelectFilter::make('status')
                    ->label('وضعیت')
                    ->options([
                        AdminMessage::STATUS_DRAFT => 'پیش‌نویس',
                        AdminMessage::STATUS_SENT => 'ارسال شده',
                    ]),
                SelectFilter::make('channel')
                    ->label('مدل ارسال')
                    ->options([
                        AdminMessage::CHANNEL_SMS => 'پیامک',
                        AdminMessage::CHANNEL_EMAIL => 'ایمیل',
                        AdminMessage::CHANNEL_INTERNAL => 'داخلی',
                    ]),
                SelectFilter::make('project_id')
                    ->label('پروژه')
                    ->relationship('project', 'name')
                    ->getOptionLabelFromRecordUsing(fn ($record) => SelectOptionLabels::project($record))
                    ->searchable()
                    ->preload(),
                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;
                    }),
            ])
            ->defaultSort('created_at', 'desc');
    }
}
