<?php

namespace App\Filament\Resources\Items\Schemas;

use Filament\Forms\Components\KeyValue;
use Filament\Forms\Components\TagsInput;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;

class ItemForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make('Producto')
                    ->columns(2)
                    ->schema([
                        TextInput::make('name')
                            ->label('Nombre')
                            ->required()
                            ->columnSpanFull(),
                        TextInput::make('sku')
                            ->label('SKU'),
                        TextInput::make('category')
                            ->label('Categoría'),
                        TextInput::make('color')
                            ->label('Color'),
                        TextInput::make('uom')
                            ->label('Unidad de medida')
                            ->default('pcs'),
                        Textarea::make('description')
                            ->label('Descripción')
                            ->rows(3)
                            ->columnSpanFull(),
                    ]),

                Section::make('Precio')
                    ->columns(3)
                    ->schema([
                        TextInput::make('unit_price')
                            ->label('Precio unitario')
                            ->numeric()
                            ->default(0)
                            ->prefix('$'),
                        TextInput::make('cost')
                            ->label('Costo')
                            ->numeric()
                            ->prefix('$'),
                        TextInput::make('moq')
                            ->label('MOQ')
                            ->numeric()
                            ->default(1),
                    ]),

                Section::make('Comprador')
                    ->columns(2)
                    ->schema([
                        TextInput::make('buyer_code')
                            ->label('Código del comprador'),
                        TextInput::make('buyer_reference')
                            ->label('Referencia del comprador'),
                    ]),

                Section::make('Detalles')
                    ->schema([
                        // images es text[] en Postgres, no JSON: TagsInput
                        // entrega y recibe un array de PHP, que es lo que
                        // espera el cast PostgresArray.
                        TagsInput::make('images')
                            ->label('Imágenes (URLs)')
                            ->placeholder('Pega una URL y pulsa Enter'),
                        // specs es jsonb con cast 'array'.
                        KeyValue::make('specs')
                            ->label('Especificaciones')
                            ->keyLabel('Campo')
                            ->valueLabel('Valor'),
                        Toggle::make('is_active')
                            ->label('Activo')
                            ->default(true),
                    ]),
            ]);
    }
}
