<?php

namespace App\Filament\Resources\OpeningBalances\Schemas;

use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Textarea;
use Filament\Schemas\Schema;

class OpeningBalanceForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Select::make('product_id')
                    ->label('الصنف')
                    ->relationship('product', 'name')
                    ->required()
                    ->searchable()
                    ->preload()
                    ->live(),
                    
                Select::make('warehouse_id')
                    ->label('المخزن')
                    ->relationship('warehouse', 'name')
                    ->required()
                    ->searchable()
                    ->preload(),
                    
                DatePicker::make('date')
                    ->label('تاريخ الرصيد')
                    ->required()
                    ->default(now())
                    ->native(false),
                    
                TextInput::make('quantity')
                    ->label('الكمية')
                    ->helperText(function ($get) {
                        $product = \App\Models\Product::find($get('product_id'));
                        return $product ? 'بالوحدة الأساسية: ' . $product->baseUnit->name : 'حدد الصنف أولاً';
                    })
                    ->required()
                    ->numeric()
                    ->minValue(0)
                    ->step(1)
                    ->suffix(fn ($get) => \App\Models\Product::find($get('product_id'))?->baseUnit->name ?? '')
                    ->live(),
                    
                Textarea::make('notes')
                    ->label('ملاحظات')
                    ->rows(3)
                    ->columnSpanFull(),
            ]);
    }
}
