<?php

namespace App\Filament\Resources;

use App\Models\HomepageSection;
use BackedEnum;
use UnitEnum;
use Filament\Forms;
use Filament\Schemas\Schema;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Actions\EditAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\BulkActionGroup;
use Filament\Forms\Components\Builder;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Tabs;

class HomepageSectionResource extends Resource
{
    protected static ?string $model = HomepageSection::class;

    // Exact types from HasNavigation trait in Filament Resource
    protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-document-duplicate';
    protected static string|UnitEnum|null $navigationGroup = 'Site Management';
    protected static ?string $label = 'Page Content';

    protected static ?string $pluralLabel = 'Page Sections';

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->schema([
                Section::make('Page & Identity')
                    ->columns(2)
                    ->schema([
                        Select::make('page_slug')
                            ->options([
                                'home' => 'Home Page',
                                'about' => 'About Us',
                                'contact' => 'Contact Page',
                            ])
                            ->required()
                            ->default('home'),
                        TextInput::make('section_key')
                            ->helperText('Unique identifier for this section (e.g., hero, why_us)')
                            ->required(),
                        TextInput::make('title')
                            ->maxLength(255),
                        TextInput::make('subtitle')
                            ->maxLength(255),
                        Toggle::make('is_active')
                            ->default(true),
                        TextInput::make('sort_order')
                            ->numeric()
                            ->default(0),
                    ]),

                Section::make('Section Builder')
                    ->description('Industry-level zero-code block management')
                    ->schema([
                        Builder::make('content')
                            ->blocks([
                                // ─────────────────────────────────────
                                // HERO BLOCK
                                // ─────────────────────────────────────
                                Builder\Block::make('hero')
                                    ->icon('heroicon-o-sparkles')
                                    ->schema([
                                        TextInput::make('headline_line_1')->label('Line 1')->required(),
                                        TextInput::make('headline_line_2')->label('Line 2'),
                                        TextInput::make('headline_accent')->label('Accent Text')->required(),
                                        Textarea::make('description')->rows(3),
                                        TextInput::make('primary_cta_text')->default('Start Registration'),
                                        TextInput::make('primary_cta_link')->default('/register'),
                                        TextInput::make('secondary_cta_text')->default('WhatsApp Us'),
                                        TextInput::make('secondary_cta_link')->default('https://wa.me/...'),
                                        TextInput::make('online_status_text')->default('Expert Team Online — Registrations Open'),
                                    ]),

                                // ─────────────────────────────────────
                                // BENTO WHY BLOCK
                                // ─────────────────────────────────────
                                Builder\Block::make('why_bento')
                                    ->label('Bento Why Cards')
                                    ->icon('heroicon-o-square-3-stack-3d')
                                    ->schema([
                                        TextInput::make('eyebrow')->default('Why Texora'),
                                        TextInput::make('headline')->default('We handle all legal work.'),
                                        Repeater::make('items')
                                            ->schema([
                                                TextInput::make('icon')->placeholder('emoji (e.g., ⚡)'),
                                                TextInput::make('title')->required(),
                                                Textarea::make('description')->required(),
                                            ])
                                            ->grid(2)
                                            ->maxItems(4),
                                    ]),

                                // ─────────────────────────────────────
                                // STICKY SERVICES BLOCK
                                // ─────────────────────────────────────
                                Builder\Block::make('sticky_services')
                                    ->label('Industry Sticky Scroll')
                                    ->icon('heroicon-o-arrows-up-down')
                                    ->schema([
                                        TextInput::make('section_title')->default('Most Ordered Services (05)'),
                                        Repeater::make('panels')
                                            ->schema([
                                                TextInput::make('title')->required(),
                                                TextInput::make('subtitle')->required(),
                                                Textarea::make('description')->required(),
                                                TextInput::make('price_label')->placeholder('e.g., Start from PKR 1,500'),
                                                TextInput::make('slug')->label('Service Slug')->helperText('For linking to the details page'),
                                                FileUpload::make('mock_image')
                                                    ->label('Preview Image')
                                                    ->image()
                                                    ->directory('cms/sticky'),
                                                Repeater::make('features')
                                                    ->schema([
                                                        TextInput::make('text')->placeholder('Feature...'),
                                                    ])
                                                    ->simple(TextInput::make('text')),
                                            ])
                                            ->itemLabel(fn (array $state): ?string => $state['title'] ?? null)
                                            ->collapsible(),
                                    ]),

                                // ─────────────────────────────────────
                                // GLOBAL CARDS BLOCK
                                // ─────────────────────────────────────
                                Builder\Block::make('global_expansion')
                                    ->icon('heroicon-o-globe-alt')
                                    ->schema([
                                        TextInput::make('eyebrow')->default('International Business'),
                                        TextInput::make('headline')->default('Expand Your Business Globally.'),
                                        Grid::make(2)->schema([
                                            Section::make('USA Card')->schema([
                                                TextInput::make('usa_title')->default('USA LLC Formation'),
                                                Repeater::make('usa_features')->simple(TextInput::make('text')),
                                                TextInput::make('usa_price')->default('From $299'),
                                            ]),
                                            Section::make('UK Card')->schema([
                                                TextInput::make('uk_title')->default('UK LTD Incorporation'),
                                                Repeater::make('uk_features')->simple(TextInput::make('text')),
                                                TextInput::make('uk_price')->default('From £49'),
                                            ]),
                                        ]),
                                    ]),

                                // ─────────────────────────────────────
                                // PROTOCOL / STEPS BLOCK
                                // ─────────────────────────────────────
                                Builder\Block::make('how_it_works')
                                    ->icon('heroicon-o-list-bullet')
                                    ->schema([
                                        TextInput::make('eyebrow')->default('How It Works'),
                                        TextInput::make('headline')->default('Simple steps. Real results.'),
                                        Repeater::make('steps')
                                            ->schema([
                                                TextInput::make('icon')->placeholder('emoji'),
                                                TextInput::make('title')->required(),
                                                Textarea::make('description')->required(),
                                            ])
                                            ->maxItems(3),
                                    ]),
                            ])
                            ->collapsible()
                            ->collapsed()
                            ->cloneable(),
                    ]),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('page_slug')
                    ->badge()
                    ->searchable(),
                Tables\Columns\TextColumn::make('section_key')
                    ->searchable()
                    ->weight('bold'),
                Tables\Columns\IconColumn::make('is_active')
                    ->boolean(),
                Tables\Columns\TextColumn::make('sort_order')
                    ->sortable(),
                Tables\Columns\TextColumn::make('updated_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                Tables\Filters\SelectFilter::make('page_slug')
                    ->options([
                        'home' => 'Home Page',
                    ]),
            ])
            ->recordActions([
                EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ])
            ->reorderable('sort_order')
            ->defaultSort('sort_order');
    }

    public static function getRelations(): array
    {
        return [];
    }

    public static function getPages(): array
    {
        return [
            'index' => HomepageSections\Pages\ListHomepageSections::route('/'),
            'create' => HomepageSections\Pages\CreateHomepageSection::route('/create'),
            'edit' => HomepageSections\Pages\EditHomepageSection::route('/{record}/edit'),
        ];
    }
}
