<?php

namespace App\Filament\Resources;

use App\Filament\Resources\EventResource\Pages;
use App\Models\Event;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Actions;
use UnitEnum;
use BackedEnum;

class EventResource extends Resource
{
    protected static ?string $model = Event::class;
    protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-calendar';
    protected static UnitEnum|string|null $navigationGroup = 'Platform';
    protected static ?int $navigationSort = 20;

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('id')->sortable(),
                Tables\Columns\TextColumn::make('tenant_id')->label('Tenant')->sortable(),
                Tables\Columns\TextColumn::make('name')->limit(30),
                Tables\Columns\TextColumn::make('slug')->searchable(),
                Tables\Columns\TextColumn::make('date')->date(),
                Tables\Columns\IconColumn::make('is_active')->boolean(),
                Tables\Columns\TextColumn::make('default_locale'),
                Tables\Columns\TextColumn::make('join')->label('Join')
                    ->getStateUsing(fn($record) => url("/e/{$record->slug}"))
                    ->copyable()
                    ->copyMessage('Join link copied'),
                Tables\Columns\TextColumn::make('created_at')->since(),
            ])
            ->filters([])
            ->actions([
                Actions\EditAction::make(),
                Actions\Action::make('toggle')
                    ->label('Toggle Active')
                    ->icon('heroicon-o-power')
                    ->action(fn($record) => $record->update(['is_active' => !$record->is_active])),
                Actions\Action::make('join_link')
                    ->label('Join Link / QR')
                    ->icon('heroicon-o-qr-code')
                    ->modalHeading('Event Join Link')
                    ->modalSubmitActionLabel('Close')
                    ->modalContent(fn($record) => view('filament.events.join-link', [
                        'link' => url("/e/{$record->slug}"),
                    ])),
            ])
            ->bulkActions([
                Actions\DeleteBulkAction::make(),
            ]);
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListEvents::route('/'),
            'view' => Pages\ViewEvent::route('/{record}'),
            'edit' => Pages\EditEvent::route('/{record}/edit'),
        ];
    }
}