<?php

namespace App\Filament\Resources\Pages\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\RichEditor;
use Filament\Schemas\Components\Utilities\Set;
use Filament\Schemas\Schema;
use Illuminate\Support\Str;
use App\Models\Category;

class PageForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Tabs::make('Page Details')
                    ->tabs([
                        Tabs\Tab::make('General')
                            ->schema([
                                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),
                            ])->columns(2),

                        Tabs\Tab::make('SEO')
                            ->schema([
                                TextInput::make('meta_title'),
                                Textarea::make('meta_description')->rows(3),
                            ]),

                        Tabs\Tab::make('Page Sections Builder')
                            ->schema([
                                Builder::make('sections_builder')
                                    ->label('Page Blocks')
                                    ->blocks([
                                        Builder\Block::make('hero')
                                            ->label('Hero Slider')
                                            ->icon('heroicon-o-presentation-chart-bar')
                                            ->schema([
                                                Repeater::make('slides')
                                                    ->schema([
                                                        TextInput::make('title')->required(),
                                                        TextInput::make('subtitle'),
                                                        TextInput::make('button_text'),
                                                        TextInput::make('button_url'),
                                                        FileUpload::make('image_path')
                                                            ->disk(config('filesystems.default', 'public'))
                                                            ->directory('page-builder')
                                                            ->image()
                                                            ->required(),
                                                    ])
                                                    ->minItems(1)
                                                    ->columnSpanFull(),
                                            ]),

                                        Builder\Block::make('category_grid')
                                            ->label('Category Grid')
                                            ->icon('heroicon-o-squares-2x2')
                                            ->schema([
                                                TextInput::make('title')->required(),
                                                Select::make('category_ids')
                                                    ->label('Categories')
                                                    ->multiple()
                                                    ->options(fn () => Category::pluck('name', 'id')->toArray())
                                                    ->required(),
                                            ]),

                                        Builder\Block::make('product_collection')
                                            ->label('Product Collection')
                                            ->icon('heroicon-o-shopping-bag')
                                            ->schema([
                                                TextInput::make('title')->required(),
                                                Select::make('collection_type')
                                                    ->options([
                                                        'latest' => 'Latest Products',
                                                        'featured' => 'Featured Products',
                                                        'discounted' => 'Discounted/Sale Products',
                                                    ])
                                                    ->required(),
                                                TextInput::make('limit')
                                                    ->numeric()
                                                    ->default(8)
                                                    ->required(),
                                            ])->columns(2),

                                        Builder\Block::make('promo_banner')
                                            ->label('Promo Banner')
                                            ->icon('heroicon-o-gift')
                                            ->schema([
                                                TextInput::make('title'),
                                                TextInput::make('description'),
                                                TextInput::make('button_text'),
                                                TextInput::make('button_url'),
                                                FileUpload::make('image_path')
                                                    ->disk(config('filesystems.default', 'public'))
                                                    ->directory('page-builder')
                                                    ->image(),
                                            ])->columns(2),

                                        Builder\Block::make('faq')
                                            ->label('FAQ Accordion')
                                            ->icon('heroicon-o-question-mark-circle')
                                            ->schema([
                                                TextInput::make('title')->default('Frequently Asked Questions'),
                                                Repeater::make('faqs')
                                                    ->schema([
                                                        TextInput::make('question')->required(),
                                                        Textarea::make('answer')->required(),
                                                    ])
                                                    ->minItems(1)
                                                    ->columnSpanFull(),
                                            ]),

                                        Builder\Block::make('rich_text')
                                            ->label('Rich Text Editor')
                                            ->icon('heroicon-o-document-text')
                                            ->schema([
                                                RichEditor::make('content')
                                                    ->required()
                                                    ->columnSpanFull(),
                                            ]),

                                        Builder\Block::make('testimonials')
                                            ->label('Testimonials')
                                            ->icon('heroicon-o-chat-bubble-bottom-center-text')
                                            ->schema([
                                                TextInput::make('title')->default('What Our Customers Say'),
                                                Repeater::make('testimonials')
                                                    ->schema([
                                                        TextInput::make('name')->required(),
                                                        TextInput::make('role')->placeholder('CEO, Acme Corp'),
                                                        Textarea::make('feedback')->required()->rows(3),
                                                        Select::make('rating')
                                                            ->options([
                                                                5 => '5 Stars',
                                                                4 => '4 Stars',
                                                                3 => '3 Stars',
                                                                2 => '2 Stars',
                                                                1 => '1 Star',
                                                            ])
                                                            ->default(5)
                                                            ->required(),
                                                        FileUpload::make('avatar')
                                                            ->disk(config('filesystems.default', 'public'))
                                                            ->directory('page-builder')
                                                            ->image(),
                                                    ])
                                                    ->minItems(1)
                                                    ->columnSpanFull(),
                                            ]),

                                        Builder\Block::make('newsletter_signup')
                                            ->label('Newsletter Signup')
                                            ->icon('heroicon-o-envelope')
                                            ->schema([
                                                TextInput::make('title')->default('Subscribe to Our Newsletter')->required(),
                                                TextInput::make('subtitle')->default('Get the latest updates, deals, and exclusive offers sent directly to your inbox.'),
                                                TextInput::make('placeholder_text')->default('Enter your email address')->required(),
                                                TextInput::make('button_text')->default('Subscribe')->required(),
                                            ])->columns(2),

                                        Builder\Block::make('video_embed')
                                            ->label('Video Embed')
                                            ->icon('heroicon-o-video-camera')
                                            ->schema([
                                                TextInput::make('title'),
                                                TextInput::make('video_url')
                                                    ->label('Video URL (YouTube or Vimeo)')
                                                    ->required()
                                                    ->placeholder('e.g., https://www.youtube.com/watch?v=dQw4w9WgXcQ'),
                                                Select::make('aspect_ratio')
                                                    ->options([
                                                        '16:9' => '16:9 (Standard)',
                                                        '4:3' => '4:3',
                                                        '1:1' => '1:1 (Square)',
                                                    ])
                                                    ->default('16:9')
                                                    ->required(),
                                            ])->columns(2),
                                    ])
                                    ->columnSpanFull(),
                            ]),
                    ])->columnSpanFull(),
            ]);
    }
}
