<?php

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

use App\Enums\RoleEnum;
use App\Enums\VerificationStatus;
use App\Filament\Actions\Cheerful\ForceDeleteAction;
use App\Filament\Actions\Cheerful\RestoreAction;
use App\Filament\Columns\RowIndexColumn;
use App\Filament\Columns\TimestampColumns;
use App\Filament\Pages\Media;
use App\Filament\Resources\Manage\Journalists\Actions\DeleteJournalistAction;
use App\Filament\Resources\Manage\Journalists\Actions\EditJournalistAction;
use App\Filament\Support\CheerfulNotification;
use Filament\Actions\Action;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\ForceDeleteBulkAction;
use Filament\Actions\RestoreBulkAction;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;

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

                TextColumn::make('name')
                    ->label('Nama')
                    ->searchable()
                    ->sortable(),

                TextColumn::make('email')
                    ->label('Alamat Surel')
                    ->searchable()
                    ->sortable(),

                TextColumn::make('phone_number')
                    ->label('Nomor Telepon')
                    ->searchable()
                    ->sortable(),

                TextColumn::make('press_card')
                    ->label('Dokumen Pers')
                    ->searchable()
                    ->sortable()
                    ->wrap(),

                TextColumn::make('ukw_certificate')
                    ->label('Sertifikat UKW')
                    ->searchable()
                    ->sortable()
                    ->wrap(),

                ...TimestampColumns::make(),
            ])
            ->filters([
                TrashedFilter::make()
                    ->native(false)
                    ->visible(fn () => auth()->user()?->hasRole(RoleEnum::DEVELOPER->value)),
            ])
            ->checkIfRecordIsSelectableUsing(
                fn () => ! (
                    in_array(auth()->user()->company?->verificationRequest?->status, [VerificationStatus::PENDING, VerificationStatus::REJECTED]) ||
                    auth()->user()->company?->verificationRequest()->pending()->exists()
                )
            )
            ->recordActions([
                EditJournalistAction::make('edit')
                    ->visible(fn () => ! (in_array(auth()->user()->company?->verificationRequest?->status, [VerificationStatus::PENDING, VerificationStatus::REJECTED]) || auth()->user()->company?->verificationRequest()->pending()->exists())),

                DeleteJournalistAction::make('delete')
                    ->visible(fn () => ! (in_array(auth()->user()->company?->verificationRequest?->status, [VerificationStatus::PENDING, VerificationStatus::REJECTED]) || auth()->user()->company?->verificationRequest()->pending()->exists())),

                ForceDeleteAction::make()
                    ->visible(fn () => ! (in_array(auth()->user()->company?->verificationRequest?->status, [VerificationStatus::PENDING, VerificationStatus::REJECTED]) || auth()->user()->company?->verificationRequest()->pending()->exists())),

                RestoreAction::make()
                    ->visible(fn () => ! (in_array(auth()->user()->company?->verificationRequest?->status, [VerificationStatus::PENDING, VerificationStatus::REJECTED]) || auth()->user()->company?->verificationRequest()->pending()->exists())),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),

                    ForceDeleteBulkAction::make(),

                    RestoreBulkAction::make(),
                ])->visible(fn () => ! (in_array(auth()->user()->company?->verificationRequest?->status, [VerificationStatus::PENDING, VerificationStatus::REJECTED]) || auth()->user()->company?->verificationRequest()->pending()->exists())),
            ])
            ->emptyStateIcon(Heroicon::OutlinedIdentification)
            ->emptyStateHeading(fn () => CheerfulNotification::getByKey('journalist.empty_state_heading'))
            ->emptyStateDescription(fn () => CheerfulNotification::getByKey('journalist.empty_state'))
            ->defaultSort('created_at', 'desc')
            ->deferFilters(false)
            ->paginated([25, 50, 100, 'all'])
            ->deferLoading()
            ->when(fn () => auth()->user()->hasRole(RoleEnum::PERUSAHAAN->value) && ! auth()->user()->company?->partnerMedia, function (Table $table): Table {
                return $table
                    ->emptyStateHeading(fn () => CheerfulNotification::getByKey('access.restricted'))
                    ->emptyStateDescription(fn () => CheerfulNotification::getByKey('access.complete_media_data'))
                    ->emptyStateIcon(Heroicon::OutlinedLockClosed)
                    ->emptyStateActions([
                        Action::make('complete_media')
                            ->label('Lengkapi Data Media')
                            ->url(Media::getUrl())
                            ->button(),
                    ]);
            });
    }
}
