<?php

declare(strict_types=1);

namespace App\Filament\Resources\Articles\Schemas;

use App\Domain\Blog\ContentFormat;
use App\Domain\Plugin\Hook;
use App\Settings\GeneralSettings;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Schema;

class ArticleForm
{
    public static function configure(Schema $schema): Schema
    {
        $defaultFormat = ContentFormat::MARKDOWN;
        try {
            $defaultFormat = ContentFormat::normalize(app(GeneralSettings::class)->default_content_format, ContentFormat::MARKDOWN);
        } catch (\Throwable) {
            //
        }

        return $schema
            ->components([
                Select::make('category_id')
                    ->label(__('admin.fields.category'))
                    ->relationship('category', 'name')
                    ->required(),
                Select::make('user_id')
                    ->label(__('admin.fields.author'))
                    ->relationship('user', 'name')
                    ->required(),
                TextInput::make('title')
                    ->label(__('admin.fields.title'))
                    ->required()
                    ->columnSpanFull(),
                Select::make('content_format')
                    ->label(__('admin.fields.content_format'))
                    ->options([
                        ContentFormat::MARKDOWN => __('admin.options.markdown_recommended'),
                        ContentFormat::HTML => __('admin.options.html_legacy'),
                    ])
                    ->default($defaultFormat)
                    ->required()
                    ->live()
                    ->helperText(__('admin.helpers.article_content')),
                Textarea::make('content')
                    ->label(fn (Get $get): string => $get('content_format') === ContentFormat::MARKDOWN
                        ? __('admin.fields.content_markdown')
                        : __('admin.fields.content_html'))
                    ->required()
                    ->rows(18)
                    ->columnSpanFull(),
                TextInput::make('description')
                    ->label(__('admin.fields.description')),
                TextInput::make('keywords')
                    ->label(__('admin.fields.keywords')),
                TextInput::make('slug')
                    ->label(__('admin.fields.slug')),
                DateTimePicker::make('published_at')
                    ->label(__('admin.fields.published_at'))
                    ->default(now()),
                Toggle::make('stick')
                    ->label(__('admin.fields.stick'))
                    ->default(false),
                Toggle::make('visible')
                    ->label(__('admin.fields.visible'))
                    ->default(true),
                Toggle::make('close_comment')
                    ->label(__('admin.fields.close_comment'))
                    ->default(false),
                TextInput::make('read_password')
                    ->label(__('admin.fields.read_password'))
                    ->password(),
                TextInput::make('cover_path')
                    ->label(__('admin.fields.cover_path'))
                    ->helperText(__('admin.helpers.cover_path'))
                    ->columnSpanFull(),
                TextInput::make('cover_source')
                    ->label(__('admin.fields.cover_source'))
                    ->disabled()
                    ->dehydrated(false),
                TextInput::make('cover_status')
                    ->label(__('admin.fields.cover_status'))
                    ->disabled()
                    ->dehydrated(false),
                Textarea::make('ai_summary')
                    ->label(__('admin.fields.ai_summary'))
                    ->rows(3)
                    ->columnSpanFull(),
                Textarea::make('ai_polished_content')
                    ->label(__('admin.fields.ai_polished_content'))
                    ->helperText(__('admin.helpers.ai_polished_content'))
                    ->rows(12)
                    ->columnSpanFull(),
                Textarea::make('ai_suggestions_text')
                    ->label(__('admin.fields.ai_suggestions'))
                    ->helperText(__('admin.helpers.ai_suggestions'))
                    ->rows(4)
                    ->disabled()
                    ->dehydrated(false)
                    ->columnSpanFull(),
                ...Hook::collect('filament.article.form'),
            ]);
    }
}
