<?php

namespace App\Filament\Resources\BlogPosts\Schemas;

use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Schema;

class BlogPostForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                \Filament\Forms\Components\Grid::make(3)
                    ->schema([
                        \Filament\Forms\Components\Group::make()
                            ->schema([
                                \Filament\Forms\Components\Section::make('Post Content')
                                    ->schema([
                                        TextInput::make('title')
                                            ->required()
                                            ->lazy()
                                            ->afterStateUpdated(fn (string $operation, $state, \Filament\Forms\Set $set) => $operation === 'create' ? $set('slug', \Illuminate\Support\Str::slug($state)) : null),
                                        TextInput::make('slug')
                                            ->required()
                                            ->unique(\App\Models\BlogPost::class, 'slug', ignoreRecord: true),
                                        \Filament\Forms\Components\RichEditor::make('content')
                                            ->required()
                                            ->columnSpanFull(),
                                        Textarea::make('excerpt')
                                            ->rows(3)
                                            ->columnSpanFull(),
                                    ]),
                                \Filament\Forms\Components\Section::make('SEO Metadata')
                                    ->collapsed()
                                    ->schema([
                                        TextInput::make('seo_metadata.title')
                                            ->label('Meta Title'),
                                        Textarea::make('seo_metadata.description')
                                            ->label('Meta Description'),
                                    ]),
                            ])
                            ->columnSpan(2),

                        \Filament\Forms\Components\Group::make()
                            ->schema([
                                \Filament\Forms\Components\Section::make('Status & Media')
                                    ->schema([
                                        Toggle::make('is_published')
                                            ->label('Published')
                                            ->inline(false),
                                        DateTimePicker::make('published_at')
                                            ->default(now()),
                                        FileUpload::make('featured_image')
                                            ->image()
                                            ->directory('blog')
                                            ->imageEditor(),
                                    ]),
                                \Filament\Forms\Components\Section::make('Classification')
                                    ->schema([
                                        \Filament\Forms\Components\Select::make('user_id')
                                            ->label('Author')
                                            ->relationship('author', 'name')
                                            ->searchable()
                                            ->default(auth()->id())
                                            ->required(),
                                        \Filament\Forms\Components\Select::make('blog_category_id')
                                            ->label('Category')
                                            ->relationship('category', 'name')
                                            ->searchable(),
                                        \Filament\Forms\Components\Select::make('tags')
                                            ->multiple()
                                            ->relationship('tags', 'name')
                                            ->preload(),
                                        TextInput::make('read_time')
                                            ->numeric()
                                            ->suffix('mins'),
                                    ]),
                            ])
                            ->columnSpan(1),
                    ]),
            ]);
    }
}
