<?php

namespace App\Filament\Tenant\Resources\PartResource\RelationManagers;

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

class BatchesRelationManager extends RelationManager
{
    protected static string $relationship = 'batches';

    protected static ?string $title = 'Loturi (FIFO)';

    public function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('received_at')
                    ->label('Recepție')
                    ->dateTime('d.m.Y H:i')
                    ->sortable(),
                Tables\Columns\TextColumn::make('warehouse.code')->label('Depozit')->placeholder('—'),
                Tables\Columns\TextColumn::make('batch_ref')->label('Ref.')->placeholder('—'),
                Tables\Columns\TextColumn::make('supplier.name')->label('Furnizor')->placeholder('—'),
                Tables\Columns\TextColumn::make('qty_in')
                    ->label('Intrat')
                    ->numeric(decimalPlaces: 2)
                    ->alignRight(),
                Tables\Columns\TextColumn::make('qty_remaining')
                    ->label('Rămas')
                    ->numeric(decimalPlaces: 2)
                    ->alignRight()
                    ->weight('bold')
                    ->color(fn ($state) => (float) $state <= 0 ? 'gray' : 'success'),
                Tables\Columns\TextColumn::make('buy_price')
                    ->label('Preț unit.')
                    ->money('MDL')
                    ->alignRight(),
            ])
            ->defaultSort('received_at')
            ->emptyStateHeading('Niciun lot înregistrat')
            ->emptyStateDescription('Apasă „Recepție" pe lista de piese pentru a înregistra prima intrare în depozit.');
    }
}
