<?php

namespace App\Filament\Tenant\Widgets;

use App\Models\Tenant\Part;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget as BaseWidget;

class LowStockTable extends BaseWidget
{
    protected static ?int $sort = 5;

    protected int|string|array $columnSpan = 'full';

    protected static ?string $heading = '⚠️ Stoc minim atins';

    public function table(Table $table): Table
    {
        return $table
            ->query(
                Part::query()
                    ->where('is_active', true)
                    ->whereColumn('qty', '<=', 'min_qty')
                    ->orderBy('qty')
            )
            ->columns([
                Tables\Columns\TextColumn::make('name')->label('Piesă')->wrap()->searchable(),
                Tables\Columns\TextColumn::make('article')->label('Cod')->placeholder('—'),
                Tables\Columns\TextColumn::make('qty')->label('Stoc')->alignRight()
                    ->color(fn ($state) => $state <= 0 ? 'danger' : 'warning')
                    ->weight('bold'),
                Tables\Columns\TextColumn::make('min_qty')->label('Min.')->alignRight(),
                Tables\Columns\TextColumn::make('preferredSupplier.name')->label('Furnizor')->placeholder('—'),
            ])
            ->paginated(false)
            ->defaultSort('qty');
    }
}
