<?php

namespace App\Filament\Resources\BlogPosts\Schemas;

use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Components\Callout;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\Tabs;
use Filament\Schemas\Components\Tabs\Tab;
use Filament\Schemas\Schema;
use Illuminate\Support\HtmlString;

class BlogPostForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Tabs::make('Blog Post Builder')
                    ->persistTabInQueryString('blog_tab')
                    ->columnSpanFull()
                    ->tabs([
                        Tab::make('Content')
                            ->icon('heroicon-m-newspaper')
                            ->schema([
                                Callout::make('Database synced blog')
                                    ->info()
                                    ->description('Published posts appear on the homepage resources section, the blog index, and the public post URL automatically.'),

                                Section::make('Post identity')
                                    ->description('Control the title, custom slug, status, and publish date.')
                                    ->columns(2)
                                    ->schema([
                                        TextInput::make('title')
                                            ->required()
                                            ->maxLength(255)
                                            ->placeholder('How to monetize short links without hurting UX'),

                                        TextInput::make('slug')
                                            ->required()
                                            ->unique(ignoreRecord: true)
                                            ->maxLength(255)
                                            ->prefix('/blog/')
                                            ->helperText('Custom public URL slug for this blog post.')
                                            ->placeholder('monetize-short-links-without-hurting-ux'),

                                        Select::make('status')
                                            ->options([
                                                'draft' => 'Draft',
                                                'published' => 'Published',
                                            ])
                                            ->native(false)
                                            ->required()
                                            ->default('draft'),

                                        DateTimePicker::make('published_at')
                                            ->seconds(false)
                                            ->helperText('Leave empty to publish immediately when status is Published.'),
                                    ]),

                                Section::make('Featured image')
                                    ->description('Upload the post cover image. It is stored on the public disk and displayed on the homepage, blog index, and post page.')
                                    ->schema([
                                        FileUpload::make('featured_image_path')
                                            ->label('Featured image')
                                            ->disk('public')
                                            ->directory('blog')
                                            ->visibility('public')
                                            ->image()
                                            ->imagePreviewHeight('180')
                                            ->helperText('Recommended size: 1200x675px.'),
                                    ]),

                                Section::make('Post body')
                                    ->description('Write the article with the WYSIWYG HTML editor.')
                                    ->schema([
                                        Textarea::make('excerpt')
                                            ->rows(4)
                                            ->maxLength(280)
                                            ->helperText('Short summary used on blog cards and social fallbacks.')
                                            ->columnSpanFull(),

                                        RichEditor::make('content')
                                            ->label('WYSIWYG HTML content')
                                            ->helperText(new HtmlString('Use headings, links, lists, quotes, and images inside the editor. The stored HTML renders on the public blog post page.'))
                                            ->required()
                                            ->columnSpanFull(),
                                    ]),
                            ]),

                        Tab::make('SEO')
                            ->icon('heroicon-m-magnifying-glass')
                            ->schema([
                                Section::make('Search metadata')
                                    ->description('Set ranking and social preview details for this blog post.')
                                    ->columns(2)
                                    ->schema([
                                        TextInput::make('seo_title')
                                            ->maxLength(180)
                                            ->helperText('Recommended: 50-60 characters. Falls back to post title.'),

                                        Textarea::make('seo_description')
                                            ->rows(5)
                                            ->maxLength(220)
                                            ->helperText('Recommended: 140-160 characters. Falls back to excerpt.')
                                            ->columnSpanFull(),

                                        TextInput::make('seo_keywords')
                                            ->maxLength(255)
                                            ->helperText('Comma-separated keywords.'),
                                    ]),

                                Grid::make(2)
                                    ->schema([
                                        TextInput::make('og_title')
                                            ->label('Open Graph title')
                                            ->maxLength(180)
                                            ->helperText('Optional social title. Falls back to SEO title.'),

                                        Textarea::make('og_description')
                                            ->label('Open Graph description')
                                            ->rows(5)
                                            ->maxLength(220)
                                            ->helperText('Optional social description. Falls back to SEO description.')
                                            ->columnSpanFull(),
                                    ]),
                            ]),
                    ]),
            ]);
    }
}
