<?php

namespace App\Filament\Resources\Pages\Schemas;

use Filament\Forms;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\Tabs;
use Filament\Schemas\Schema;
use Illuminate\Support\Str;

class PageForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Tabs::make('Page Content')
                    ->tabs([
                        Tabs\Tab::make('Content')
                            ->icon('heroicon-o-document-text')
                            ->schema([
                                Section::make('Basic Information')
                                    ->schema([
                                        Forms\Components\TextInput::make('title')
                                            ->label('Title')
                                            ->required()
                                            ->maxLength(255)
                                            ->live(onBlur: true)
                                            ->afterStateUpdated(fn ($state, callable $set) => $set('slug', Str::slug($state)))
                                            ->prefixIcon('heroicon-o-document-text')
                                            ->placeholder('Enter page title'),
                                        
                                        Forms\Components\TextInput::make('slug')
                                            ->label('Slug')
                                            ->required()
                                            ->maxLength(255)
                                            ->unique(ignoreRecord: true)
                                            ->prefixIcon('heroicon-o-link')
                                            ->placeholder('page-url-slug')
                                            ->helperText('URL-friendly version of the title'),
                                        
                                        Forms\Components\Textarea::make('summary')
                                            ->label('Summary')
                                            ->rows(3)
                                            ->maxLength(1000)
                                            ->placeholder('Brief summary of the page')
                                            ->helperText('Short description for previews and listings')
                                            ->columnSpanFull(),
                                        
                                        Forms\Components\RichEditor::make('description')
                                            ->label('Description')
                                            ->placeholder('Full page content...')
                                            ->toolbarButtons([
                                                'bold',
                                                'italic',
                                                'underline',
                                                'strike',
                                                'link',
                                                'h2',
                                                'h3',
                                                'bulletList',
                                                'orderedList',
                                                'blockquote',
                                                'codeBlock',
                                                'undo',
                                                'redo',
                                            ])
                                            ->columnSpanFull(),
                                    ])
                                    ->columns(2),
                                
                                Section::make('Media')
                                    ->schema([
                                        Forms\Components\FileUpload::make('thumbnail')
                                            ->label('Thumbnail')
                                            ->image()
                                            ->imageEditor()
                                            ->disk('public')
                                            ->directory('pages/thumbnails')
                                            ->maxSize(2048)
                                            ->helperText('Small preview image')
                                            ->imagePreviewHeight('150')
                                            ->columnSpanFull(),
                                        
                                        Forms\Components\FileUpload::make('image')
                                            ->label('Featured Image')
                                            ->image()
                                            ->imageEditor()
                                            ->disk('public')
                                            ->directory('pages/images')
                                            ->maxSize(5120)
                                            ->helperText('Main page image')
                                            ->imagePreviewHeight('150')
                                            ->columnSpanFull(),
                                    ])
                                    ->columns(1)
                                    ->collapsible(),
                            ]),
                        
                        Tabs\Tab::make('SEO')
                            ->icon('heroicon-o-magnifying-glass')
                            ->schema([
                                Section::make('Search Engine Optimization')
                                    ->description('Optimize your page for search engines.')
                                    ->schema([
                                        Forms\Components\TextInput::make('meta_title')
                                            ->label('Meta Title')
                                            ->maxLength(60)
                                            ->placeholder('SEO title (60 characters max)')
                                            ->helperText('Leave empty to use page title')
                                            ->columnSpanFull(),
                                        
                                        Forms\Components\Textarea::make('meta_description')
                                            ->label('Meta Description')
                                            ->rows(3)
                                            ->maxLength(160)
                                            ->placeholder('SEO description (160 characters max)')
                                            ->helperText('Brief description for search results')
                                            ->columnSpanFull(),
                                        
                                        Forms\Components\Textarea::make('meta_keywords')
                                            ->label('Meta Keywords')
                                            ->rows(2)
                                            ->placeholder('keyword1, keyword2, keyword3')
                                            ->helperText('Comma-separated keywords')
                                            ->columnSpanFull(),
                                    ]),
                            ]),
                        
                        Tabs\Tab::make('Settings')
                            ->icon('heroicon-o-cog-6-tooth')
                            ->schema([
                                Section::make('Publishing')
                                    ->schema([
                                        Forms\Components\Toggle::make('is_published')
                                            ->label('Published')
                                            ->helperText('Make this page visible to the public')
                                            ->default(false),
                                        
                                        Forms\Components\DateTimePicker::make('published_at')
                                            ->label('Publish Date')
                                            ->helperText('Schedule when this page goes live')
                                            ->native(false),
                                        
                                        Forms\Components\TextInput::make('order')
                                            ->label('Display Order')
                                            ->numeric()
                                            ->default(0)
                                            ->helperText('Lower numbers appear first')
                                            ->minValue(0),
                                    ])
                                    ->columns(3),
                            ]),
                    ])
                    ->columnSpanFull(),
            ]);
    }
}
