<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

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]);
    }
}
