<?php

namespace App\Filament\Resources\LandingPages\Schemas;

use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Components\Tabs;
use Filament\Forms\Components\Builder;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\ColorPicker;
use Filament\Schemas\Components\Utilities\Set;
use Filament\Schemas\Schema;
use Illuminate\Support\Str;
use App\Models\Product;

class LandingPageForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Tabs::make('Landing Page Details')
                    ->tabs([
                        Tabs\Tab::make('General')
                            ->schema(static::getGeneralSchema())
                            ->columns(2),

                        Tabs\Tab::make('Linked Product & Pricing')
                            ->schema(static::getProductSchema())
                            ->columns(2),

                        Tabs\Tab::make('Design & Theme')
                            ->schema(static::getDesignSchema())
                            ->columns(2),

                        Tabs\Tab::make('Sections Builder')
                            ->schema([
                                static::getSectionsBuilder()->columnSpanFull(),
                            ]),
                    ])->columnSpanFull(),
            ]);
    }

    public static function getGeneralSchema(): array
    {
        return [
            TextInput::make('title')
                ->required()
                ->live(onBlur: true)
                ->afterStateUpdated(function (Set $set, ?string $state) {
                    $set('slug', Str::slug($state));
                }),
            TextInput::make('slug')
                ->required()
                ->unique(ignoreRecord: true),
            Toggle::make('is_active')
                ->label('Active')
                ->default(true),
        ];
    }

    public static function getProductSchema(): array
    {
        return [
            Select::make('product_id')
                ->label('Linked Product')
                ->options(fn () => Product::pluck('name', 'id')->toArray())
                ->searchable()
                ->required(),
            TextInput::make('landing_price')
                ->label('Special Offer Price (BDT)')
                ->numeric()
                ->prefix('৳')
                ->helperText('If set, this price overrides the default product price on the landing page.'),
            TextInput::make('fb_pixel_id')
                ->label('Facebook Pixel ID')
                ->placeholder('e.g., 123456789012345')
                ->helperText('Custom Meta Pixel for tracking ad conversions specifically for this landing page.'),
        ];
    }

    public static function getDesignSchema(): array
    {
        return [
            Select::make('template_style')
                ->label('Template Design Style')
                ->options([
                    'minimalist' => '📱 Minimalist Tech & Gadget',
                    'organic' => '🍃 Soft Organic & Beauty',
                    'bold' => '🔥 Bold Sales & Direct Response',
                ])
                ->default('minimalist')
                ->required()
                ->live(),
            Select::make('font_family')
                ->label('Typography Font')
                ->options([
                    'Inter' => 'Inter (Modern Sans-Serif)',
                    'Outfit' => 'Outfit (Clean & Premium)',
                    'Hind Siliguri' => 'Hind Siliguri (Bengali Standard)',
                    'Anek Bangla' => 'Anek Bangla (Bengali Dynamic/Bold)',
                ])
                ->default('Inter')
                ->required(),
            ColorPicker::make('primary_color')
                ->label('Primary Theme Color')
                ->default('#4f46e5')
                ->required(),
            ColorPicker::make('secondary_color')
                ->label('Secondary / Gradient Color')
                ->default('#06b6d4')
                ->required(),
            TextInput::make('gradient_angle')
                ->label('Gradient Angle (Degrees)')
                ->numeric()
                ->default(135)
                ->required(),
        ];
    }

    public static function getSectionsBuilder(): Builder
    {
        \Pboivin\FilamentPeek\Facades\Peek::registerBuilderEditor();

        return Builder::make('sections_builder')
            ->label('Page Blocks')
            ->hintAction(
                \Filament\Actions\Action::make('open_live_editor')
                    ->label('Live Visual Editor')
                    ->icon('heroicon-o-pencil-square')
                    ->link()
                    ->visible(fn ($livewire) => ! $livewire instanceof \Pboivin\FilamentPeek\Livewire\BuilderEditor)
                    ->action(function ($livewire) {
                        if ($livewire instanceof \Pboivin\FilamentPeek\Livewire\BuilderEditor) {
                            return;
                        }
                        \Pboivin\FilamentPeek\Facades\Peek::ensurePluginIsLoaded();
                        \Pboivin\FilamentPeek\Facades\Peek::registerBuilderEditor();
                        \Pboivin\FilamentPeek\Facades\Peek::ensurePageSupportsBuilderPreview($livewire);
                        $livewire->openPreviewModalForBuilder('sections_builder');
                    })
            )
            ->blocks([
                Builder\Block::make('hero')
                    ->label('Hero Banner')
                    ->icon('heroicon-o-presentation-chart-bar')
                    ->schema([
                        TextInput::make('headline')->required(),
                        Textarea::make('subheadline')->rows(2),
                        FileUpload::make('image_path')
                            ->label('Main Product Image')
                            ->disk(config('filesystems.default', 'public'))
                            ->directory('landing-pages')
                            ->image(),
                        TextInput::make('video_url')
                            ->label('Header Video URL (YouTube)')
                            ->placeholder('e.g., https://www.youtube.com/watch?v=xxx'),
                        TextInput::make('cta_text')
                            ->label('Call to Action Button Text')
                            ->default('অর্ডার করুন'),
                    ])->columns(2),

                Builder\Block::make('features')
                    ->label('Product Features')
                    ->icon('heroicon-o-squares-2x2')
                    ->schema([
                        TextInput::make('title')->default('প্রোডাক্টের বিশেষ সুবিধাসমূহ'),
                        Repeater::make('features_list')
                            ->schema([
                                TextInput::make('title')->required(),
                                Textarea::make('description')->rows(2)->required(),
                                FileUpload::make('icon_path')
                                    ->disk(config('filesystems.default', 'public'))
                                    ->directory('landing-pages')
                                    ->image(),
                            ])
                            ->minItems(1)
                            ->columnSpanFull(),
                    ]),

                Builder\Block::make('video_embed')
                    ->label('Video Showcase')
                    ->icon('heroicon-o-video-camera')
                    ->schema([
                        TextInput::make('title')->default('ভিডিওতে আমাদের প্রোডাক্ট দেখুন'),
                        TextInput::make('video_url')
                            ->label('YouTube Video URL')
                            ->required()
                            ->placeholder('https://www.youtube.com/watch?v=xxx'),
                    ]),

                Builder\Block::make('faq')
                    ->label('FAQ Accordion')
                    ->icon('heroicon-o-question-mark-circle')
                    ->schema([
                        TextInput::make('title')->default('সাধারণ কিছু প্রশ্ন ও উত্তর (FAQ)'),
                        Repeater::make('faqs')
                            ->schema([
                                TextInput::make('question')->required(),
                                Textarea::make('answer')->required(),
                            ])
                            ->minItems(1)
                            ->columnSpanFull(),
                    ]),

                Builder\Block::make('testimonials')
                    ->label('Customer Reviews')
                    ->icon('heroicon-o-chat-bubble-bottom-center-text')
                    ->schema([
                        TextInput::make('title')->default('আমাদের কাস্টমাররা কী বলছেন'),
                        Repeater::make('testimonials_list')
                            ->schema([
                                TextInput::make('name')->required(),
                                Textarea::make('feedback')->required()->rows(3),
                                Select::make('rating')
                                    ->options([
                                        5 => '⭐⭐⭐⭐⭐ (5 Stars)',
                                        4 => '⭐⭐⭐⭐ (4 Stars)',
                                        3 => '⭐⭐⭐ (3 Stars)',
                                    ])
                                    ->default(5)
                                    ->required(),
                                FileUpload::make('avatar')
                                    ->disk(config('filesystems.default', 'public'))
                                    ->directory('landing-pages')
                                    ->image(),
                            ])
                            ->minItems(1)
                            ->columnSpanFull(),
                    ]),

                Builder\Block::make('express_checkout')
                    ->label('🛒 Embedded Checkout Form')
                    ->icon('heroicon-o-shopping-bag')
                    ->schema([
                        TextInput::make('form_title')->default('অর্ডার কনফার্ম করতে ফর্মটি পূরণ করুন'),
                        TextInput::make('button_text')->default('অর্ডার সাবমিট করুন'),
                        Toggle::make('enable_bkash')
                            ->label('Allow bKash Payment')
                            ->default(true),
                        Toggle::make('enable_cod')
                            ->label('Allow Cash on Delivery')
                            ->default(true),
                    ])->columns(2),

                Builder\Block::make('custom_html')
                    ->label('Custom HTML / CSS Code')
                    ->icon('heroicon-o-code-bracket')
                    ->schema([
                        Textarea::make('code')
                            ->label('HTML / CSS / JS code')
                            ->rows(8)
                            ->placeholder('Paste Elementor code, raw HTML, CSS styles, or external tracking script here')
                            ->required(),
                    ]),
            ]);
    }
}
