<?php

declare(strict_types=1);

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