<?php

declare(strict_types=1);

namespace App\Filament\Resources\Pages;

use App\Filament\Resources\Pages\Pages\ListPages;
use App\Models\Page;
use BackedEnum;
use Filament\Actions\CreateAction;
use Filament\Actions\EditAction;
use Filament\Forms\Components\MarkdownEditor;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Resources\Resource;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Support\Str;
use UnitEnum;

/**
 * Content the studio writes itself: studio rules, terms, privacy.
 *
 * The rules were promised "later", and terms will need revising the first
 * time a solicitor reads them. Neither should be a deployment.
 *
 * Markdown rather than a rich-text editor on purpose: the body is rendered
 * with Str::markdown(), which escapes inline HTML, so nothing pasted in here
 * can become a script tag on a public page.
 */
class PageResource extends Resource
{
    protected static ?string $model = Page::class;

    protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedDocumentText;

    protected static string|UnitEnum|null $navigationGroup = 'Studio setup';

    protected static ?string $recordTitleAttribute = 'title';

    protected static ?int $navigationSort = 5;

    public static function form(Schema $schema): Schema
    {
        return $schema->components([
            Section::make()
                ->schema([
                    TextInput::make('title')
                        ->required()
                        ->live(onBlur: true)
                        ->afterStateUpdated(function ($state, $set, $get, string $operation) {
                            // Only on create. Changing the slug of a live page
                            // breaks every link already printed or shared.
                            if ($operation === 'create') {
                                $set('slug', Str::slug((string) $state));
                            }
                        }),
                    TextInput::make('slug')
                        ->required()
                        ->unique(ignoreRecord: true)
                        ->helperText('The page lives at /p/{slug}. Changing it breaks existing links.'),
                    TextInput::make('summary')
                        ->maxLength(255)
                        ->columnSpanFull()
                        ->helperText('One line shown under the heading.'),
                    MarkdownEditor::make('body')
                        ->columnSpanFull()
                        ->helperText('Markdown. Headings, lists, bold and links all work.'),
                ])
                ->columns(2),

            Section::make('Visibility')
                ->schema([
                    Toggle::make('is_published')
                        ->label('Published')
                        ->helperText('Unpublished pages return a 404.'),
                    Toggle::make('show_in_footer')->label('Link from the footer'),
                    TextInput::make('sort_order')->numeric()->default(0),
                ])
                ->columns(3),
        ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->defaultSort('sort_order')
            ->columns([
                TextColumn::make('title')->searchable()->weight('bold')
                    ->description(fn (Page $record) => '/p/'.$record->slug),
                IconColumn::make('is_published')->label('Published')->boolean(),
                IconColumn::make('show_in_footer')->label('In footer')->boolean(),
                TextColumn::make('updated_at')->label('Updated')->since()->toggleable(),
            ])
            ->headerActions([CreateAction::make()])
            ->recordActions([EditAction::make()])
            ->toolbarActions([]);
    }

    public static function getPages(): array
    {
        return ['index' => ListPages::route('/')];
    }
}
