<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpInventorySerialNumbers\RelationManagers;

use App\Business\Inventory\Enums\StocktakeDifferenceStatus;
use App\Business\Inventory\Enums\StocktakeSerialNumberDifferenceType;
use App\Filament\Resources\DberpStocktakeOrders\DberpStocktakeOrderResource;
use App\Support\DberpInventorySerialNumberPresentation;
use Filament\Actions\Action;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Model;

class StocktakeSerialNumberDifferencesRelationManager extends RelationManager
{
    protected static string $relationship = 'stocktakeSerialNumberDifferences';

    public static function getTitle(Model $ownerRecord, string $pageClass): string
    {
        return __('dberp.relations.stocktake_serial_number_differences');
    }

    protected static function getModelLabel(): ?string
    {
        return __('dberp.fields.stocktake_serial_number_difference');
    }

    protected static function getPluralModelLabel(): ?string
    {
        return __('dberp.relations.stocktake_serial_number_differences');
    }

    public function form(Schema $schema): Schema
    {
        return $schema;
    }

    public function table(Table $table): Table
    {
        return $table
            ->modifyQueryUsing(function ($query) {
                $owner = $this->getOwnerRecord();

                return $query
                    ->where('product_id', $owner->product_id)
                    ->with(['stocktakeOrder', 'warehouse']);
            })
            ->defaultSort('id', 'desc')
            ->columns([
                TextColumn::make('stocktakeOrder.code')
                    ->label(__('dberp.fields.stocktake_order'))
                    ->searchable(),
                TextColumn::make('difference_type')
                    ->label(__('dberp.fields.difference_type'))
                    ->badge()
                    ->formatStateUsing(fn (?string $state): string => StocktakeSerialNumberDifferenceType::tryFrom((string) $state)?->label() ?? (string) $state),
                TextColumn::make('system_status')
                    ->label(__('dberp.fields.system_status_snapshot'))
                    ->formatStateUsing(fn (?string $state): string => DberpInventorySerialNumberPresentation::statusLabel($state)),
                TextColumn::make('status')
                    ->label(__('dberp.fields.handling_status'))
                    ->badge()
                    ->formatStateUsing(fn (?string $state): string => StocktakeDifferenceStatus::tryFrom((string) $state)?->label() ?? (string) $state),
                TextColumn::make('created_at')
                    ->label(__('dberp.fields.created_at'))
                    ->dateTime(),
            ])
            ->recordActions([
                Action::make('view_stocktake')
                    ->label(__('dberp.actions.view_stocktake_order'))
                    ->url(fn (Model $record): string => DberpStocktakeOrderResource::getUrl('edit', ['record' => $record->stocktake_order_id]))
                    ->visible(fn (): bool => DberpStocktakeOrderResource::canViewAny()),
            ])
            ->emptyStateHeading(__('dberp.empty_states.inventory_serial_number_no_stocktake_differences'))
            ->paginated([10, 25, 50]);
    }
}
