<?php

namespace App\Filament\Resources\Products\Schemas;

use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Placeholder;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;

class ProductAttributeForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Grid::make(3)
                    ->schema([
                        // Left column: main info (span 2)
                        Grid::make(1)
                            ->schema([
                                Section::make('Attribute Info')
                                    ->description('Define the attribute name (e.g. Size, Color).')
                                    ->schema([
                                        TextInput::make('name')
                                            ->required()
                                            ->unique(ignoreRecord: true)
                                            ->placeholder('e.g., Size, Color, Material'),
                                    ]),

                                Section::make('Attribute Options / Values')
                                    ->description('Define the options available for this attribute.')
                                    ->schema([
                                        Repeater::make('values')
                                            ->relationship('values')
                                            ->schema([
                                                TextInput::make('value')
                                                    ->required()
                                                    ->placeholder('e.g., XL, Blue, Cotton'),
                                            ])
                                            ->grid(2)
                                            ->defaultItems(1)
                                            ->columnSpanFull()
                                            ->addActionLabel('Add Option Value'),
                                    ]),
                            ])
                            ->columnSpan(2),

                        // Right column: sidebar (span 1)
                        Grid::make(1)
                            ->schema([
                                Section::make('Record Metadata')
                                    ->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(),
            ]);
    }
}
