<?php

namespace App\Filament\Resources\MenuResource\RelationManagers;

use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;

class ItemsRelationManager extends RelationManager
{
    protected static string $relationship = 'items';

    public function form(Form $form): Form
    {
        return $form->schema([
            Forms\Components\TextInput::make('label')->required()->maxLength(120),
            Forms\Components\Select::make('page_id')
                ->relationship('page', 'title')
                ->searchable()->preload()
                ->helperText('Link to a CMS page, or leave empty and set a URL below.'),
            Forms\Components\TextInput::make('url')
                ->placeholder('/blog or https://...')
                ->maxLength(500),
            Forms\Components\Toggle::make('new_tab')->label('Open in new tab'),
            Forms\Components\TextInput::make('sort')->numeric()->default(0),
        ])->columns(2);
    }

    public function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('label'),
                Tables\Columns\TextColumn::make('page.title')->placeholder('—'),
                Tables\Columns\TextColumn::make('url')->limit(40)->placeholder('—'),
                Tables\Columns\TextColumn::make('sort'),
            ])
            ->defaultSort('sort')
            ->reorderable('sort')
            ->headerActions([Tables\Actions\CreateAction::make()])
            ->actions([
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make(),
            ]);
    }
}
