<?php

declare(strict_types=1);

namespace App\Filament\Resources\Comments\Schemas;

use App\Domain\Blog\ArticleExcerpt;
use App\Models\Comment;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Schema;

class CommentForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Select::make('article_id')
                    ->label(__('admin.fields.article'))
                    ->relationship('article', 'title')
                    ->required()
                    ->live(),
                Textarea::make('article_excerpt_preview')
                    ->label(__('admin.fields.article_excerpt'))
                    ->disabled()
                    ->dehydrated(false)
                    ->rows(3)
                    ->columnSpanFull()
                    ->visible(fn (?Comment $record): bool => $record?->article !== null)
                    ->afterStateHydrated(function (Textarea $component, mixed $state, ?Comment $record): void {
                        if ($record?->article === null) {
                            return;
                        }

                        $component->state(app(ArticleExcerpt::class)->forList($record->article, 240));
                    }),
                TextInput::make('author')
                    ->label(__('admin.fields.author'))
                    ->required(),
                TextInput::make('url')
                    ->label(__('admin.fields.url'))
                    ->url(),
                Textarea::make('content')
                    ->label(__('admin.fields.content'))
                    ->required()
                    ->columnSpanFull(),
                TextInput::make('ip')
                    ->label(__('admin.fields.ip')),
                Select::make('moderation_status')
                    ->label(__('admin.fields.moderation_status'))
                    ->options([
                        Comment::STATUS_PENDING => __('admin.options.moderation.pending'),
                        Comment::STATUS_PENDING_AI => __('admin.options.moderation.pending_ai'),
                        Comment::STATUS_APPROVED => __('admin.options.moderation.approved'),
                        Comment::STATUS_REJECTED => __('admin.options.moderation.rejected'),
                        Comment::STATUS_NEEDS_HUMAN => __('admin.options.moderation.needs_human'),
                    ])
                    ->required()
                    ->default(Comment::STATUS_PENDING),
                DateTimePicker::make('published_at')
                    ->label(__('admin.fields.published_at')),
            ]);
    }
}
