<?php

declare(strict_types=1);

namespace App\Filament\Resources\MediaFlags\Tables;

use App\Models\MediaFlag;
use App\Models\MediaItem;
use Filament\Actions\Action;
use Filament\Actions\DeleteAction;
use Filament\Forms\Components\Textarea;
use Filament\Notifications\Notification;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

class MediaFlagsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->modifyQueryUsing(fn ($query) => $query->with(['mediaItem.project', 'user']))
            ->defaultSort('created_at', 'desc')
            ->columns([
                TextColumn::make('id')->label('#')->sortable(),
                TextColumn::make('mediaItem.name')->label('رسانه')->placeholder('-')->limit(30),
                TextColumn::make('mediaItem.project.name')->label('پروژه')->placeholder('-'),
                TextColumn::make('mediaItem.type')
                    ->label('نوع')
                    ->badge()
                    ->formatStateUsing(fn (?string $state) => MediaItem::typeOptions()[$state] ?? $state),
                TextColumn::make('user.getFilamentName')
                    ->label('گزارش‌دهنده')
                    ->getStateUsing(fn (MediaFlag $record) => $record->user?->getFilamentName() ?? '-'),
                TextColumn::make('reason')->label('دلیل')->limit(50)->wrap(),
                TextColumn::make('created_at')->label('تاریخ')->jalaliDateTime('Y/m/d H:i')->sortable(),
            ])
            ->recordActions([
                Action::make('reject_media')
                    ->label('رد رسانه')
                    ->icon('heroicon-o-x-mark')
                    ->color('danger')
                    ->visible(fn (MediaFlag $record) => $record->mediaItem
                        && $record->mediaItem->status !== MediaItem::STATUS_REJECTED)
                    ->form([Textarea::make('admin_reason')->label('دلیل رد')->required()->rows(2)])
                    ->action(function (MediaFlag $record, array $data): void {
                        $record->mediaItem?->update([
                            'status' => MediaItem::STATUS_REJECTED,
                            'admin_reason' => $data['admin_reason'],
                        ]);
                        Notification::make()->title('رسانه رد شد.')->danger()->send();
                    }),
                Action::make('approve_media')
                    ->label('تأیید رسانه')
                    ->icon('heroicon-o-check')
                    ->color('success')
                    ->visible(fn (MediaFlag $record) => $record->mediaItem
                        && $record->mediaItem->status !== MediaItem::STATUS_APPROVED)
                    ->action(function (MediaFlag $record): void {
                        $record->mediaItem?->update([
                            'status' => MediaItem::STATUS_APPROVED,
                            'admin_reason' => null,
                        ]);
                        Notification::make()->title('رسانه تأیید شد.')->success()->send();
                    }),
                DeleteAction::make()->label('حذف گزارش'),
            ]);
    }
}
