<?php

namespace App\Filament\Resources\Master\Themes\Tables;

use App\Enums\IsActive;
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\Theme;
use Filament\Actions\BulkActionGroup;
use Filament\Support\Enums\Width;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ToggleColumn;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;

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

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

                ToggleColumn::make('is_active')
                    ->label('Aktif?')
                    ->sortable()
                    ->getStateUsing(fn (Theme $record): bool => $record->is_active === IsActive::ACTIVE)
                    ->updateStateUsing(function (Theme $record, bool $state): void {
                        $record->is_active = $state ? IsActive::ACTIVE : IsActive::INACTIVE;
                        $record->save();

                        CheerfulNotification::statusUpdated()->send();
                    }),

                ...TimestampColumns::make(),
            ])
            ->filters([
                TrashedFilter::make()
                    ->native(false)
                    ->visible(fn () => auth()->user()?->hasRole(RoleEnum::DEVELOPER->value)),
            ])
            ->recordActions([
                EditAction::make()
                    ->modalWidth(Width::Large),

                DeleteAction::make(),

                ForceDeleteAction::make(),

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