<?php

namespace App\Filament\Resources\Posts\Schemas;

use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Schema;
use Illuminate\Support\Str;

class PostForm
{
    public static function configure(Schema $schema): Schema
    {
        $canReview = auth()->user()?->can('Publish:Post');

        return $schema->components([
            TextInput::make('title')->label('Judul')->required()
                ->live(onBlur: true)
                ->afterStateUpdated(fn ($state, $set) => $set('slug', Str::slug($state))),
            TextInput::make('slug')->required()->unique(ignoreRecord: true),
            Select::make('category')->label('Kategori')
                ->options([
                    'umum'       => 'Umum',
                    'pengumuman' => 'Pengumuman',
                    'berita'     => 'Berita',
                ])
                ->default('umum')->required(),
            DateTimePicker::make('published_at')->label('Tanggal Publikasi')
                ->visible($canReview)
                ->helperText('Kosongkan untuk menyimpan sebagai draft'),
            RichEditor::make('content')->label('Konten')->required()->columnSpanFull(),
        ]);
    }
}
