<?php

namespace App\Filament\Resources\InventarioPrendaResource\RelationManagers;

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

class DistribucionesRelationManager extends RelationManager
{
    protected static string $relationship = 'distribuciones';

    protected static ?string $recordTitleAttribute = 'id';

    public function form(Form $form): Form
    {
        return $form->schema([
            Forms\Components\Select::make('bodega_id')->relationship('bodega', 'nombre')->nullable(),
            Forms\Components\Select::make('proveedor_id')->relationship('proveedor', 'nombre')->nullable()->helperText('Opcional: asignar a un proveedor/operario en vez de bodega'),
            Forms\Components\Select::make('color_id')->relationship('color', 'name')->nullable(),
            Forms\Components\Select::make('size_id')->relationship('size', 'name')->nullable(),
            Forms\Components\TextInput::make('cantidad')
                ->numeric()
                ->required()
                ->minValue(1)
                ->default(fn() => $this->getOwnerRecord()?->cantidad_disponible ?? 0)
                ->maxValue(fn() => $this->getOwnerRecord()?->cantidad_disponible ?? 0),
        ]);
    }

    public function table(Table $table): Table
    {
        return $table->columns([
            Tables\Columns\TextColumn::make('bodega.nombre')->label('Bodega'),
            Tables\Columns\TextColumn::make('proveedor.nombre')->label('Proveedor'),
            Tables\Columns\TextColumn::make('color.name')->label('Color'),
            Tables\Columns\TextColumn::make('size.name')->label('Talla'),
            Tables\Columns\TextColumn::make('cantidad'),
            Tables\Columns\TextColumn::make('created_at')->label('Creado')->dateTime(),
        ])->filters([
            //
        ])->headerActions([
            Tables\Actions\CreateAction::make(),
        ])->actions([
            Tables\Actions\EditAction::make(),
            Tables\Actions\DeleteAction::make(),
        ]);
    }
}
