<?php

namespace App\Filament\Resources\Entries\Schemas;

use App\Models\Author;
use App\Models\Game;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Schema;

class EntryForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Select::make('type')
                    ->options([
            'translations' => 'Translations',
            'romhacks' => 'Romhacks',
            'homebrew' => 'Homebrew',
            'utilities' => 'Utilities',
            'documents' => 'Documents',
            'lua-scripts' => 'Lua scripts',
            'tutorials' => 'Tutorials',
        ])
                    ->required()
                    ->live(),
                TextInput::make('title')
                    ->maxLength(255)
                    ->required(),
                TextInput::make('slug')
                    ->maxLength(255)
                    ->unique(ignoreRecord: true)
                    ->required(),
                Textarea::make('description')
                    ->required()
                    ->columnSpanFull(),
                FileUpload::make('main_image')
                    ->image(),
                Select::make('state')
                    ->options([
            'draft' => 'Draft',
            'pending' => 'Pending',
            'published' => 'Published',
            'locked' => 'Locked',
            'hidden' => 'Hidden',
        ])
                    ->default('draft')
                    ->required(),
                Toggle::make('featured')
                    ->required(),
                Select::make('game_id')
                    ->relationship('game', 'name')
                    ->searchable()
                    ->default(null)
                    ->hidden(fn(Get $get) => $get('type') === 'homebrew')
                    ->createOptionForm([
                        TextInput::make('name')
                            ->required()
                            ->maxLength(255),
                        TextInput::make('slug')
                            ->required()
                            ->maxLength(255),
                        Select::make('platform_id')
                            ->relationship('platform', 'name')
                            ->required(),
                        Select::make('genre_id')
                            ->relationship('genre', 'name')
                            ->required(),
                    ])
                    ->createOptionUsing(function (array $data){
                       return Game::create( $data )->id;
                    })
                ,
                Select::make('platform_id')
                    ->relationship('platform', 'name')
                    ->default(null)
                    ->hidden(fn(Get $get) => in_array( $get('type'), [ 'translations', 'romhacks' ] ) ),
                Select::make('status_id')
                    ->relationship('status', 'id')
                    ->default(null),
                Select::make('authors')
                    ->relationship('authors', 'name')
                    ->searchable()
                    ->multiple()
                    ->createOptionForm([
                        TextInput::make('name')
                            ->maxLength(255)
                            ->required(),
                        TextInput::make('slug')
                            ->maxLength(255)
                            ->unique(ignoreRecord: true)
                            ->required(),
                        TextInput::make('website')
                            ->url()
                            ->maxLength(500)
                            ->default(null),
                        TextInput::make('user_id')
                            ->numeric()
                            ->default(null),
                    ])
                    ->createOptionUsing(function (array $data){
                        return Author::create( $data )->id;
                    })
                ,
                Select::make('languages')
                    ->relationship('languages', 'name')
                    ->multiple()
                    ->preload(),
                Select::make('modifications')
                    ->relationship('modifications', 'name')
                    ->multiple()
                    ->preload()
                    ->hidden(fn(Get $get) => $get('type') !== "romhacks"),
                TextInput::make('version')
                    ->default(null),
                DatePicker::make('release_date'),
                Textarea::make('staff_credits')
                    ->default(null)
                    ->columnSpanFull(),
                TextInput::make('relevant_link')
                    ->default(null),
                TextInput::make('youtube_link')
                    ->default(null),
                TextInput::make('user_id')
                    ->required()
                    ->numeric(),
                TextInput::make('comments_thread_id')
                    ->numeric()
                    ->default(null),
            ]);
    }
}
