<?php

namespace App\Filament\Resources\Categories\Schemas;

use Filament\Forms\Components\Placeholder;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Components\SpatieMediaLibraryFileUpload;
use Filament\Schemas\Components\Utilities\Set;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Illuminate\Support\Str;

class CategoryForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Grid::make(3)
                    ->schema([

                        // ── Left / Main Column (span 2) ──────────────────────
                        Grid::make(1)
                            ->schema([
                                Section::make('Category Information')
                                    ->description('Specify name, slug, and hierarchical relations.')
                                    ->schema([
                                        TextInput::make('name')
                                            ->required()
                                            ->live(onBlur: true)
                                            ->afterStateUpdated(function (Set $set, ?string $state) {
                                                $set('slug', Str::slug($state));
                                            }),
                                        TextInput::make('slug')
                                            ->required()
                                            ->unique(ignoreRecord: true),
                                        Select::make('parent_id')
                                            ->label('Parent Category')
                                            ->relationship('parent', 'name')
                                            ->placeholder('Select parent category (optional)')
                                            ->columnSpanFull(),
                                    ])
                                    ->columns(2),

                                Section::make('Description')
                                    ->schema([
                                        Textarea::make('description')
                                            ->hiddenLabel()
                                            ->rows(5)
                                            ->columnSpanFull(),
                                    ]),
                            ])
                            ->columnSpan(2),

                        // ── Right Sidebar Column (span 1) ────────────────────
                        Grid::make(1)
                            ->schema([
                                Section::make('Visibility')
                                    ->schema([
                                        Toggle::make('is_visible')
                                            ->label('Visible on Storefront')
                                            ->default(true)
                                            ->required()
                                            ->columnSpanFull(),
                                    ]),

                                Section::make('Category Image')
                                    ->schema([
                                        SpatieMediaLibraryFileUpload::make('image')
                                            ->collection('image')
                                            ->disk(config('media-library.disk_name', 'public'))
                                            ->image()
                                            ->maxSize(2048)
                                            ->helperText('Recommended size: 256x256 pixels. PNG format with transparent background is preferred.')
                                            ->columnSpanFull(),
                                    ]),

                                Section::make('Record Info')
                                    ->schema([
                                        Placeholder::make('created_at')
                                            ->label('Created at')
                                            ->content(fn ($record): string => $record?->created_at
                                                ? $record->created_at->diffForHumans()
                                                : '-'),
                                        Placeholder::make('updated_at')
                                            ->label('Last modified at')
                                            ->content(fn ($record): string => $record?->updated_at
                                                ? $record->updated_at->diffForHumans()
                                                : '-'),
                                    ])
                                    ->hidden(fn ($record) => $record === null),
                            ])
                            ->columnSpan(1),

                    ])
                    ->columnSpanFull(),
            ]);
    }
}
