<?php

namespace App\Filament\Resources\Posts\Schemas;

use App\Models\Post;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Illuminate\Support\Str;

class PostForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make('Content')
                    ->description('The article itself. Use the toolbar for headings, lists, quotes, links and inline images.')
                    ->schema([
                        TextInput::make('title')
                            ->required()
                            ->maxLength(180)
                            ->live(onBlur: true)
                            ->afterStateUpdated(fn ($state, callable $set, $get) => $get('slug') ? null : $set('slug', Str::slug($state ?? ''))),
                        TextInput::make('slug')
                            ->required()
                            ->unique(ignoreRecord: true)
                            ->helperText('The web address: /blog/your-slug. Auto-filled from the title.'),
                        Textarea::make('excerpt')
                            ->rows(2)
                            ->maxLength(300)
                            ->helperText('The teaser shown on article cards and as the default meta description.')
                            ->columnSpanFull(),
                        RichEditor::make('body')
                            ->required()
                            ->fileAttachmentsDisk('public')
                            ->fileAttachmentsDirectory('posts/inline')
                            ->extraInputAttributes(['style' => 'min-height: 28rem;'])
                            ->columnSpanFull(),
                    ])
                    ->columns(2),

                Section::make('Featured image & gallery')
                    ->description('The cover leads the article and social shares. The gallery shows as a grid under the article.')
                    ->schema([
                        FileUpload::make('cover_path')
                            ->label('Featured / cover image')
                            ->image()
                            ->disk('public')
                            ->directory('posts')
                            ->imageEditor()
                            ->imageEditorAspectRatios([null, '16:9', '4:3', '1:1'])
                            ->helperText('Crop, rotate and zoom with the built-in editor. 16:9 works best for sharing.')
                            ->columnSpanFull(),
                        FileUpload::make('gallery')
                            ->label('Image gallery (optional)')
                            ->image()
                            ->multiple()
                            ->reorderable()
                            ->appendFiles()
                            ->panelLayout('grid')
                            ->disk('public')
                            ->directory('posts/gallery')
                            ->imageEditor()
                            ->helperText('Drag to reorder. Each image can be cropped and rotated.')
                            ->columnSpanFull(),
                    ]),

                Section::make('Search & social (SEO)')
                    ->description('How this article appears on Google and when shared on WhatsApp, Facebook and X.')
                    ->collapsed()
                    ->schema([
                        TextInput::make('meta_title')
                            ->label('SEO title')
                            ->maxLength(70)
                            ->helperText('Leave blank to use the article title. Aim for under 60 characters.'),
                        TextInput::make('meta_description')
                            ->label('Meta description')
                            ->maxLength(300)
                            ->helperText('Leave blank to use the excerpt. Aim for 150 to 160 characters.'),
                    ])
                    ->columns(1),

                Section::make('Publishing')
                    ->schema([
                        TextInput::make('category')
                            ->datalist(fn () => Post::query()->whereNotNull('category')->distinct()->orderBy('category')->pluck('category')->all())
                            ->placeholder('e.g. Money & Mindset, Investing, Family & Money')
                            ->helperText('Pick an existing one from the list, or type a new category.'),
                        DateTimePicker::make('published_at')
                            ->seconds(false)
                            ->helperText('Empty = draft. A past date = live now. A future date = scheduled to auto-publish.'),
                    ])
                    ->columns(2),
            ]);
    }
}
