<?php

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

use App\Enums\MediaClassification;
use App\Enums\MediaType;
use App\Enums\RoleEnum;
use App\Enums\VerificationStatus;
use App\Filament\Actions\DefaultBulkActions;
use App\Filament\Columns\RowIndexColumn;
use App\Filament\Support\CheerfulNotification;
use App\Models\User;
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;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\HtmlString;

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

                TextColumn::make('company.name')
                    ->label('Perusahaan')
                    ->searchable(query: function ($query, $search) {
                        $query->whereHas('company', function ($q) use ($search) {
                            $q->where('companies.name', 'like', "%{$search}%")
                                ->orWhere('companies.director_name', 'like', "%{$search}%");
                        });
                    })
                    ->sortable()
                    ->description(
                        fn (User $record) => new HtmlString(
                            'Akun: '.$record->name.
                                '<br/>Direktur: '.$record->company?->director_name
                        )
                    ),

                TextColumn::make('company.partnerMedia.name')
                    ->label('Media')
                    ->searchable(query: function ($query, $search) {
                        $query->whereHas('company.partnerMedia', function ($q) use ($search) {
                            $q->where('partner_media.name', 'like', "%{$search}%");
                        });
                    })
                    ->sortable(),

                TextColumn::make('company.journalists.name')
                    ->label('Jurnalis')
                    ->searchable(query: function ($query, $search) {
                        $query->whereHas('company.journalists', function ($q) use ($search) {
                            $q->where('journalists.name', 'like', "%{$search}%");
                        });
                    })
                    ->sortable()
                    ->badge(),

                TextColumn::make('status')
                    ->label('Status')
                    ->badge()
                    ->getStateUsing(function ($record) {
                        return $record->company->verificationRequest
                            ? $record->company->verificationRequest->status->getLabel()
                            : 'Belum Mengajukan';
                    })
                    ->color(function ($record) {
                        return $record->company->verificationRequest
                            ? $record->company->verificationRequest->status->getColor()
                            : 'gray';
                    }),

                TextColumn::make('created_at')
                    ->label('Tanggal Daftar')
                    ->date()
                    ->sortable()
                    ->dateTime('l, d F Y H:i')
                    ->wrap(),

                TextColumn::make('company.verificationRequest.created_at')
                    ->label('Tanggal Pengajuan Verifikasi')
                    ->dateTime('l, d F Y H:i')
                    ->sortable()
                    ->wrap(),

                TextColumn::make('company.verificationRequest.latestReview.created_at')
                    ->label('Tanggal Verifikasi')
                    ->dateTime('l, d F Y H:i')
                    ->sortable()
                    ->wrap(),

                TextColumn::make('company.verificationRequest.latestReview.created_at')
                    ->label('Tanggal Verifikasi')
                    ->dateTime('l, d F Y H:i')
                    ->sortable()
                    ->wrap(),

                TextColumn::make('company.verificationRequest.latestReview.note')
                    ->label('Alasan Penolakan')
                    ->wrap(),

            ])
            ->filters([
                SelectFilter::make('media_type')
                    ->label('Jenis Media')
                    ->options(MediaType::class)
                    ->query(fn (Builder $query, array $data) => $query->when(
                        $data['value'],
                        fn (Builder $query, $value) => $query->whereHas('company.partnerMedia', fn ($q) => $q->where('type', $value))
                    ))
                    ->native(false),

                SelectFilter::make('media_classification')
                    ->label('Klasifikasi Media')
                    ->options(MediaClassification::class)
                    ->query(fn (Builder $query, array $data) => $query->when(
                        $data['value'],
                        fn (Builder $query, $value) => $query->whereHas('company.partnerMedia', fn ($q) => $q->where('classification', $value))
                    ))
                    ->native(false),

                SelectFilter::make('verification_status')
                    ->label('Status Verifikasi')
                    ->options(VerificationStatus::class)
                    ->query(fn (Builder $query, array $data) => $query->when(
                        $data['value'],
                        fn (Builder $query, $value) => $query->whereHas('company.verificationRequest', fn ($q) => $q->where('status', $value))
                    ))
                    ->native(false),

                TrashedFilter::make()
                    ->native(false)
                    ->visible(fn () => auth()->user()?->hasRole(RoleEnum::DEVELOPER->value)),
            ])
            ->recordActions([
                ViewAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    ...DefaultBulkActions::make('rekanan'),
                ]),
            ])
            ->emptyStateIcon(Heroicon::OutlinedBuildingOffice2)
            ->emptyStateHeading(fn () => CheerfulNotification::getByKey('company.partner_empty_heading'))
            ->emptyStateDescription(fn () => CheerfulNotification::getByKey('company.partner_empty'))
            ->defaultSort('created_at', 'desc')
            ->deferFilters(false)
            ->paginated([25, 50, 100, 'all'])
            ->deferLoading();
    }
}
