<?php

namespace App\Filament\SupplierPortal\Resources\ProductStocks\Tables;

use App\Enums\StockStatusEnum;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ToggleColumn;
use Filament\Tables\Table;

class ProductStocksTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('name')
                    ->label('Termék neve')
                    ->searchable()
                    ->sortable(),

                TextColumn::make('sku')
                    ->label('Cikkszám')
                    ->searchable(),

                ToggleColumn::make('stock_status')
                    ->label('Raktáron')
                    ->onColor('success')
                    ->offColor('danger')
                    ->getStateUsing(fn ($record) => $record->stock_status === StockStatusEnum::InStock)
                    ->updateStateUsing(function ($record, bool $state) {
                        $record->stock_status = $state
                            ? StockStatusEnum::InStock
                            : StockStatusEnum::OutOfStock;
                        $record->save();
                    }),
            ]);
    }
}
