<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpInventorySerialNumbers\RelationManagers;

use App\Support\DberpInventorySerialNumberPresentation;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Model;

class InventorySerialNumberLogsRelationManager extends RelationManager
{
    protected static string $relationship = 'logs';

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

    protected static function getModelLabel(): ?string
    {
        return __('dberp.audit.entity.DberpInventorySerialNumberLog');
    }

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

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

    public function table(Table $table): Table
    {
        return $table
            ->modifyQueryUsing(fn ($query) => $query->with('warehouse'))
            ->defaultSort('business_time', 'desc')
            ->columns([
                TextColumn::make('business_time')
                    ->label(__('dberp.fields.business_time'))
                    ->dateTime()
                    ->sortable(),
                TextColumn::make('business_type')
                    ->label(__('dberp.fields.business_type'))
                    ->badge()
                    ->formatStateUsing(fn (?string $state): string => __('dberp.options.inventory_business_type')[$state] ?? (string) $state),
                TextColumn::make('business_number')
                    ->label(__('dberp.fields.business_number'))
                    ->searchable(),
                TextColumn::make('previous_status')
                    ->label(__('dberp.fields.previous_status'))
                    ->badge()
                    ->color(fn (?string $state): string => DberpInventorySerialNumberPresentation::statusColor($state))
                    ->formatStateUsing(fn (?string $state): string => DberpInventorySerialNumberPresentation::statusLabel($state)),
                TextColumn::make('current_status')
                    ->label(__('dberp.fields.current_status'))
                    ->badge()
                    ->color(fn (?string $state): string => DberpInventorySerialNumberPresentation::statusColor($state))
                    ->formatStateUsing(fn (?string $state): string => DberpInventorySerialNumberPresentation::statusLabel($state)),
                TextColumn::make('warehouse.name')
                    ->label(__('dberp.fields.warehouse'))
                    ->placeholder('—'),
                TextColumn::make('source_summary')
                    ->label(__('dberp.fields.source_document'))
                    ->state(fn (Model $record): string => DberpInventorySerialNumberPresentation::logSourceSummary($record))
                    ->wrap(),
                TextColumn::make('operation_summary')
                    ->label(__('dberp.fields.operation_summary'))
                    ->state(fn (Model $record): string => DberpInventorySerialNumberPresentation::logOperationSummary($record))
                    ->wrap()
                    ->limit(80),
                TextColumn::make('remark')
                    ->label(__('dberp.fields.remark'))
                    ->limit(40)
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->paginated([10, 25, 50]);
    }
}
