<?php

namespace App\Filament\Resources\LandingPages\Tables;

use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Actions\Action;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ToggleColumn;
use Filament\Tables\Table;

class LandingPagesTable
{
    public static function configure(Table $table): Table
    {
        $frontendUrl = env('NEXTJS_FRONTEND_URL', 'http://localhost:3000');

        return $table
            ->columns([
                TextColumn::make('title')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('slug')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('product.name')
                    ->label('Linked Product')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('template_style')
                    ->label('Template Theme')
                    ->badge()
                    ->colors([
                        'primary' => 'minimalist',
                        'success' => 'organic',
                        'warning' => 'bold',
                    ])
                    ->sortable(),
                ToggleColumn::make('is_active')
                    ->label('Active'),
                TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                //
            ])
            ->actions([
                \Filament\Actions\Action::make('live_edit')
                    ->label('Edit')
                    ->icon('heroicon-o-pencil-square')
                    ->color('warning')
                    ->action(function ($record, \App\Filament\Resources\LandingPages\Pages\ListLandingPages $livewire) {
                        logger('Table Action live_edit Triggered', ['record_id' => $record->id]);
                        session()->put('peek_active_record_id', $record->id);
                        session()->put('peek_builder_editor_auto_refresh', true);

                        \Pboivin\FilamentPeek\Facades\Peek::ensurePluginIsLoaded();
                        \Pboivin\FilamentPeek\Facades\Peek::registerPreviewModal();
                        \Pboivin\FilamentPeek\Facades\Peek::registerBuilderEditor();

                        logger('Dispatched openPreviewModalForBuilder');
                        $livewire->openPreviewModalForBuilder('sections_builder');
                    }),
                \Filament\Actions\Action::make('preview')
                    ->label('Preview')
                    ->icon('heroicon-o-eye')
                    ->color('info')
                    ->url(fn ($record) => "{$frontendUrl}/landing/{$record->slug}?preview=1")
                    ->openUrlInNewTab(),
            ])
            ->bulkActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }
}
