<?php

namespace App\Filament\Resources\Products\Schemas;

use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\Builder;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Select;
use Filament\Schemas\Schema;
use Filament\Schemas\Components\Section;
use Filament\Forms\Components\Builder\Block;
use App\Models\Ingredient;

class ProductForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
        ->components([
            Section::make()
                ->schema([
                    TextInput::make('category')
                        ->label('Kategoria')
                        ->required()
                        ->maxLength(2048),
                    TextInput::make('description')
                        ->label('Opis')
                        ->nullable()
                        ->maxLength(2048),
                ]),
            Section::make()
                ->schema([
                    Builder::make('products')
                            ->label('Produkty')
                            ->addActionLabel('Dodaj produkt')
                            ->collapsible()
                            ->collapsed()
                            ->reorderable()
                            ->blocks([
                                Block::make('product')
                                    ->label(function (?array $state): string {
                                        if ($state === null) {
                                            return 'Produkt';
                                        }
                                
                                        return $state['name'] ?? 'Produkt';
                                    })
                                    ->schema([
                                        TextInput::make('name')
                                            ->label('Nazwa')
                                            ->required(),
                                        Select::make('ingredients')
                                            ->label('Składniki')
                                            ->multiple()
                                            ->options(Ingredient::pluck('name', 'id'))
                                            ->searchable(),
                                        TextInput::make('description')
                                            ->label('Opis')
                                            ->nullable(),
                                        TextInput::make('price_small')
                                            ->label('Cena (mała)')
                                            ->numeric()
                                            ->nullable(),
                                        TextInput::make('price_medium')
                                            ->label('Cena (średnia)')
                                            ->numeric()
                                            ->nullable(),
                                        TextInput::make('price_large')
                                            ->label('Cena (duża)')
                                            ->numeric()
                                            ->nullable(),
                                        TextInput::make('spicy')
                                            ->label('Ostrość')
                                            ->numeric()
                                            ->nullable(),
                                    ])
                                    ->columns(2),
                            ]),
                    ]),
        ]);
    }
}
