<?php

namespace App\Filament\Resources\Publication\Announcements\Tables;

use App\Enums\AnnouncementType;
use App\Enums\RoleEnum;
use App\Filament\Actions\Cheerful\DeleteAction;
use App\Filament\Actions\Cheerful\EditAction;
use App\Filament\Actions\Cheerful\ForceDeleteAction;
use App\Filament\Actions\Cheerful\RestoreAction;
use App\Filament\Actions\DefaultBulkActions;
use App\Filament\Columns\RowIndexColumn;
use App\Filament\Columns\TimestampColumns;
use App\Filament\Support\CheerfulNotification;
use App\Models\Announcement;
use Filament\Actions\BulkActionGroup;
use Filament\Support\Enums\Width;
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 AnnouncementTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                ...RowIndexColumn::make(),

                TextColumn::make('title')
                    ->label('Judul')
                    ->searchable()
                    ->sortable()
                    ->wrap(),

                TextColumn::make('content')
                    ->label('Konten')
                    ->searchable()
                    ->sortable()
                    ->wrap()
                    ->limit(100)
                    ->html()
                    ->toggleable(isToggledHiddenByDefault: true),

                TextColumn::make('type')
                    ->label('Tipe')
                    ->badge()
                    ->sortable(),

                TextColumn::make('partnerMedia')
                    ->label('Penerima')
                    ->getStateUsing(
                        fn ($record) => $record->partnerMedia->pluck('name')->toArray()
                    )
                    ->listWithLineBreaks()
                    ->limitList(3)
                    ->expandableLimitedList()
                    ->badge()
                    ->placeholder(
                        fn (Announcement $record) => $record->type === AnnouncementType::PUBLIC ? 'Publik' : 'Tidak ada penerima'
                    ),

                ...TimestampColumns::make(),
            ])
            ->filters([
                SelectFilter::make('type')
                    ->label('Tipe')
                    ->options(AnnouncementType::class)
                    ->native(false),

                TrashedFilter::make()
                    ->native(false)
                    ->visible(fn () => auth()->user()?->hasRole(RoleEnum::DEVELOPER->value)),
            ])
            ->recordActions([
                EditAction::make()
                    ->modalWidth(Width::FiveExtraLarge)
                    ->visible(fn (Announcement $record): bool => $record->type === AnnouncementType::PUBLIC),

                DeleteAction::make(),

                ForceDeleteAction::make(),

                RestoreAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    ...DefaultBulkActions::make('Pengumuman'),
                ]),
            ])
            ->emptyStateIcon(Heroicon::OutlinedMegaphone)
            ->emptyStateHeading(fn () => CheerfulNotification::getByKey('announcement.empty_state_heading'))
            ->emptyStateDescription(fn () => CheerfulNotification::getByKey('announcement.empty_state'))
            ->defaultSort('created_at', 'desc')
            ->deferFilters(false)
            ->paginated([25, 50, 100, 'all'])
            ->deferLoading();
    }
}
