<?php

namespace App\Filament\Resources\Manage\DataChanges\Tables;

use App\Enums\DataChangeStatus;
use App\Enums\RoleEnum;
use App\Filament\Actions\DefaultBulkActions;
use App\Filament\Columns\RowIndexColumn;
use App\Filament\Resources\Manage\DataChanges\Actions\AcceptAction;
use App\Filament\Resources\Manage\DataChanges\Actions\RejectAction;
use App\Filament\Support\CheerfulNotification;
use App\Models\Company;
use App\Models\DataChangeRequest;
use App\Models\Journalist;
use App\Models\PartnerMedia;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\ViewAction;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;

class DataChangeTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                ...RowIndexColumn::make(),

                TextColumn::make('user.company.name')
                    ->label('Pemohon')
                    ->searchable()
                    ->sortable()
                    ->visible(! auth()->user()->hasRole(RoleEnum::PERUSAHAAN->value)),

                TextColumn::make('entity_type')
                    ->label('Jenis Data')
                    ->searchable()
                    ->sortable()
                    ->formatStateUsing(fn (DataChangeRequest $record): ?string => match ($record->entity_type) {
                        Company::class => 'Perusahaan',
                        PartnerMedia::class => 'Media',
                        Journalist::class => 'Jurnalis',
                        default => 'Lainnya',
                    })
                    ->color(fn (DataChangeRequest $record): ?string => match ($record->entity_type) {
                        Company::class => 'success',
                        PartnerMedia::class => 'info',
                        Journalist::class => 'warning',
                        default => 'secondary',
                    })
                    ->badge(),

                TextColumn::make('status')
                    ->label('Status')
                    ->searchable()
                    ->sortable()
                    ->badge()
                    ->formatStateUsing(fn (DataChangeStatus $state): string => $state->getLabel())
                    ->color(fn (DataChangeStatus $state): string => $state->getColor())
                    ->description(fn (DataChangeRequest $record): ?string => $record?->review ? 'Oleh: '.$record->review?->reviewer?->name : ''),

                TextColumn::make('change_reason')
                    ->label('Alasan Perubahan')
                    ->limit(50),

                TextColumn::make('review.note')
                    ->label('Alasan Penolakan')
                    ->limit(50),

                TextColumn::make('created_at')
                    ->label('Tanggal Permohonan')
                    ->date()
                    ->sortable()
                    ->dateTime('l, d F Y H:i:s')
                    ->wrap(),
            ])
            ->filters([
                SelectFilter::make('status')
                    ->label('Status')
                    ->options(DataChangeStatus::class)
                    ->native(false),

                TrashedFilter::make()
                    ->native(false)
                    ->visible(fn () => auth()->user()?->hasRole(RoleEnum::DEVELOPER->value)),
            ])
            ->recordActions([
                ViewAction::make(),
                AcceptAction::make(),
                RejectAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    ...DefaultBulkActions::make('permohonan perubahan data'),
                ]),
            ])
            ->emptyStateIcon(Heroicon::OutlinedDocumentText)
            ->emptyStateHeading(fn () => CheerfulNotification::getByKey('data_change.empty_state_heading'))
            ->emptyStateDescription(fn () => CheerfulNotification::getByKey('data_change.empty_state'))
            ->defaultSort('created_at', 'desc')
            ->deferFilters(false)
            ->paginated([25, 50, 100, 'all'])
            ->deferLoading();
    }
}
