<?php

namespace App\Filament\Resources\Pages\Schemas;

use Filament\Forms\Components\CodeEditor;
use Filament\Forms\Components\CodeEditor\Enums\Language;
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 PageForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Tabs::make('CMS Page Builder')
                    ->persistTabInQueryString('cms_tab')
                    ->columnSpanFull()
                    ->tabs([
                        Tab::make('Content')
                            ->icon('heroicon-m-document-text')
                            ->schema([
                                Callout::make('Custom HTML is enabled')
                                    ->info()
                                    ->description('Admin can write full HTML for this CMS page. Use Tailwind utility classes or add page-specific CSS in the Design Code tab.'),

                                Section::make('Page identity')
                                    ->description('Control the public URL, title, and publication status.')
                                    ->columns(2)
                                    ->schema([
                                        TextInput::make('title')
                                            ->required()
                                            ->maxLength(255)
                                            ->placeholder('About urless'),

                                        TextInput::make('slug')
                                            ->required()
                                            ->unique(ignoreRecord: true)
                                            ->maxLength(255)
                                            ->prefix('/p/')
                                            ->helperText('Public page URL will use this slug. The about, faq, terms, privacy, and contact slugs also get clean public URLs.')
                                            ->placeholder('about'),

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

                                Section::make('WYSIWYG HTML page body')
                                    ->description('Build the CMS page visually. The editor stores HTML that renders inside the public page body.')
                                    ->schema([
                                        RichEditor::make('content')
                                            ->label('Page body')
                                            ->helperText(new HtmlString('Use the WYSIWYG toolbar for headings, links, lists, quotes, and tables. Add advanced styling in the Design Code tab when needed.'))
                                            ->columnSpanFull(),
                                    ]),
                            ]),

                        Tab::make('Design Code')
                            ->icon('heroicon-m-code-bracket-square')
                            ->schema([
                                Grid::make(2)
                                    ->schema([
                                        CodeEditor::make('custom_css')
                                            ->label('Custom CSS')
                                            ->language(Language::Css)
                                            ->helperText('Optional page-scoped CSS. Do not include <style> tags.')
                                            ->columnSpanFull(),

                                        CodeEditor::make('custom_js')
                                            ->label('Custom JavaScript')
                                            ->language(Language::JavaScript)
                                            ->helperText('Optional page script. Do not include <script> tags.')
                                            ->columnSpanFull(),
                                    ]),
                            ]),

                        Tab::make('SEO')
                            ->icon('heroicon-m-magnifying-glass')
                            ->schema([
                                Section::make('Search metadata')
                                    ->description('Set how this page should appear in search and social previews.')
                                    ->columns(2)
                                    ->schema([
                                        TextInput::make('seo_title')
                                            ->maxLength(70)
                                            ->helperText('Recommended: 50-60 characters.'),

                                        Textarea::make('seo_description')
                                            ->rows(5)
                                            ->maxLength(180)
                                            ->helperText('Recommended: 140-160 characters.')
                                            ->columnSpanFull(),
                                    ]),
                            ]),
                    ]),
            ]);
    }
}
