<?php

namespace App\Filament\Resources\Blogs\Schemas;

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

class BlogForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Tabs::make('Blog 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-pencil')
                                            ->placeholder('Enter blog title')
                                            ->columnSpanFull(),
                                        
                                        Forms\Components\TextInput::make('slug')
                                            ->label('Slug')
                                            ->required()
                                            ->maxLength(255)
                                            ->unique(ignoreRecord: true)
                                            ->prefixIcon('heroicon-o-link')
                                            ->placeholder('auto-generated-from-title')
                                            ->helperText('URL-friendly version of the title')
                                            ->columnSpanFull(),
                                        
                                        Forms\Components\Textarea::make('summary')
                                            ->label('Summary')
                                            ->rows(3)
                                            ->maxLength(500)
                                            ->placeholder('Brief summary of the blog post')
                                            ->helperText('Short description shown in blog listings')
                                            ->columnSpanFull(),
                                        
                                        Forms\Components\RichEditor::make('description')
                                            ->label('Content')
                                            ->required()
                                            ->placeholder('Write your blog content here...')
                                            ->helperText('Full blog post content')
                                            ->columnSpanFull()
                                            ->toolbarButtons([
                                                'bold',
                                                'italic',
                                                'underline',
                                                'strike',
                                                'link',
                                                'heading',
                                                'bulletList',
                                                'orderedList',
                                                'blockquote',
                                                'codeBlock',
                                                'undo',
                                                'redo',
                                            ]),
                                    ])
                                    ->columns(1),
                            ]),
                        
                        Tabs\Tab::make('Media')
                            ->icon('heroicon-o-photo')
                            ->schema([
                                Section::make('Featured Image')
                                    ->schema([
                                        Forms\Components\FileUpload::make('image')
                                            ->label('Featured Image')
                                            ->image()
                                            ->imageEditor()
                                            ->disk('public')
                                            ->directory('blogs')
                                            ->maxSize(5120)
                                            ->helperText('Recommended size: 1200x630 pixels')
                                            ->imagePreviewHeight('250')
                                            ->columnSpanFull(),
                                    ]),
                                
                                Section::make('Gallery')
                                    ->schema([
                                        Forms\Components\FileUpload::make('gallery')
                                            ->label('Image Gallery')
                                            ->image()
                                            ->multiple()
                                            ->reorderable()
                                            ->disk('public')
                                            ->directory('blogs/gallery')
                                            ->maxSize(5120)
                                            ->maxFiles(10)
                                            ->helperText('Upload up to 10 images for the gallery')
                                            ->imagePreviewHeight('150')
                                            ->columnSpanFull(),
                                    ])
                                    ->collapsible(),
                            ]),
                        
                        Tabs\Tab::make('Details')
                            ->icon('heroicon-o-tag')
                            ->schema([
                                Section::make('Blog Details')
                                    ->schema([
                                        Forms\Components\TextInput::make('author')
                                            ->label('Author')
                                            ->maxLength(255)
                                            ->prefixIcon('heroicon-o-user')
                                            ->placeholder('Author name')
                                            ->default(fn () => auth()->user()->name ?? null),
                                        
                                        Forms\Components\TextInput::make('category')
                                            ->label('Category')
                                            ->maxLength(255)
                                            ->prefixIcon('heroicon-o-folder')
                                            ->placeholder('e.g., Technology, Business')
                                            ->datalist([
                                                'Technology',
                                                'Business',
                                                'Design',
                                                'Marketing',
                                                'Development',
                                                'News',
                                            ]),
                                        
                                        Forms\Components\TagsInput::make('tags')
                                            ->label('Tags')
                                            ->placeholder('Add tags')
                                            ->helperText('Press Enter to add a tag')
                                            ->columnSpanFull(),
                                    ])
                                    ->columns(2),
                            ]),
                        
                        Tabs\Tab::make('SEO')
                            ->icon('heroicon-o-magnifying-glass')
                            ->schema([
                                Section::make('Search Engine Optimization')
                                    ->schema([
                                        Forms\Components\TextInput::make('meta_title')
                                            ->label('Meta Title')
                                            ->maxLength(60)
                                            ->prefixIcon('heroicon-o-document-text')
                                            ->placeholder('SEO title (60 characters max)')
                                            ->helperText('Leave empty to use blog title')
                                            ->columnSpanFull(),
                                        
                                        Forms\Components\Textarea::make('meta_description')
                                            ->label('Meta Description')
                                            ->rows(3)
                                            ->maxLength(160)
                                            ->placeholder('SEO description (160 characters max)')
                                            ->helperText('Leave empty to use summary')
                                            ->columnSpanFull(),
                                        
                                        Forms\Components\TagsInput::make('meta_keywords')
                                            ->label('Meta Keywords')
                                            ->placeholder('Add keywords')
                                            ->helperText('SEO keywords for search engines')
                                            ->columnSpanFull(),
                                    ])
                                    ->columns(1),
                            ]),
                        
                        Tabs\Tab::make('Settings')
                            ->icon('heroicon-o-cog-6-tooth')
                            ->schema([
                                Section::make('Publication Settings')
                                    ->schema([
                                        Forms\Components\Toggle::make('is_published')
                                            ->label('Published')
                                            ->helperText('Make this blog post visible to the public')
                                            ->default(false)
                                            ->live(),
                                        
                                        Forms\Components\Toggle::make('is_featured')
                                            ->label('Featured')
                                            ->helperText('Show this blog in featured section')
                                            ->default(false),
                                        
                                        Forms\Components\DateTimePicker::make('published_at')
                                            ->label('Publish Date')
                                            ->helperText('Schedule when this blog should be published')
                                            ->default(now())
                                            ->required(fn ($get) => $get('is_published')),
                                        
                                        Forms\Components\TextInput::make('order')
                                            ->label('Display Order')
                                            ->numeric()
                                            ->default(0)
                                            ->helperText('Lower numbers appear first')
                                            ->minValue(0),
                                        
                                        Forms\Components\TextInput::make('views')
                                            ->label('Views')
                                            ->numeric()
                                            ->default(0)
                                            ->disabled()
                                            ->helperText('Number of times this blog has been viewed'),
                                    ])
                                    ->columns(2),
                            ]),
                    ])
                    ->columnSpanFull(),
            ]);
    }
}
