<?php

declare(strict_types=1);

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

namespace App\Filament\Resources\DberpPurchaseOrders\RelationManagers;

use App\Business\Purchase\Enums\PurchaseExceptionResolutionResult;
use App\Business\Purchase\Enums\PurchaseExceptionStatus;
use App\Business\Purchase\Enums\PurchaseExceptionType;
use App\Filament\Resources\DberpPurchaseExceptions\DberpPurchaseExceptionResource;
use App\Support\DberpNumberFormatter;
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 ExceptionsRelationManager extends RelationManager
{
    protected static string $relationship = 'purchaseExceptions';

    public static function getTitle(Model $ownerRecord, string $pageClass): string
    {
        return '采购异常';
    }

    protected static function getModelLabel(): ?string
    {
        return '采购异常';
    }

    protected static function getPluralModelLabel(): ?string
    {
        return '采购异常';
    }

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

    public function table(Table $table): Table
    {
        return $table
            ->defaultSort('occurred_at', 'desc')
            ->columns([
                TextColumn::make('code')->label(__('dberp.fields.document_code')),
                TextColumn::make('purchase_receipt_code')->label(__('dberp.fields.purchase_receipt_code')),
                TextColumn::make('product.name')->label(__('dberp.fields.product_name')),
                TextColumn::make('exception_type')->label(__('dberp.fields.exception_type'))->badge()->formatStateUsing(fn (?string $state): string => PurchaseExceptionType::options()[$state] ?? (string) $state),
                TextColumn::make('status')->label(__('dberp.fields.exception_status'))->badge()->formatStateUsing(fn (?string $state): string => PurchaseExceptionStatus::options()[$state] ?? (string) $state),
                TextColumn::make('resolution_result')->label(__('dberp.fields.resolution_result'))->badge()->formatStateUsing(fn (?string $state): string => PurchaseExceptionResolutionResult::options()[$state] ?? (string) $state),
                TextColumn::make('difference_quantity')->label(__('dberp.fields.difference_quantity'))->formatStateUsing(fn ($state): string => DberpNumberFormatter::quantity($state)),
                TextColumn::make('pending_quantity')->label(__('dberp.fields.pending_quantity'))->formatStateUsing(fn ($state): string => DberpNumberFormatter::quantity($state)),
            ])
            ->headerActions([
                Action::make('open_exception_list')
                    ->label(__('dberp.actions.view_purchase_exception_list'))
                    ->url(fn (): string => DberpPurchaseExceptionResource::getUrl('index')),
            ])
            ->recordActions([
                Action::make('open')
                    ->label(__('dberp.actions.handle_purchase_exception'))
                    ->url(fn (Model $record): string => DberpPurchaseExceptionResource::getUrl('edit', ['record' => $record])),
            ]);
    }
}
