<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpInventoryBatches\RelationManagers;

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

class InventoryBatchMovementsRelationManager extends RelationManager
{
    protected static string $relationship = 'movements';

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

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

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

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

    public function table(Table $table): Table
    {
        return $table
            ->defaultSort('business_time', 'desc')
            ->columns([
                TextColumn::make('business_time')->label(__('dberp.fields.business_time'))->dateTime(),
                TextColumn::make('direction')->label(__('dberp.fields.direction'))->badge()
                    ->formatStateUsing(fn (?string $state): string => __('dberp.options.inventory_batch_movement_direction')[$state] ?? (string) $state),
                TextColumn::make('change_quantity')->label(__('dberp.fields.change_quantity'))->formatStateUsing(fn ($state): string => DberpNumberFormatter::quantity($state)),
                TextColumn::make('before_quantity')->label(__('dberp.fields.before_quantity'))->formatStateUsing(fn ($state): string => DberpNumberFormatter::quantity($state)),
                TextColumn::make('after_quantity')->label(__('dberp.fields.after_quantity'))->formatStateUsing(fn ($state): string => DberpNumberFormatter::quantity($state)),
                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')),
                TextColumn::make('remark')->label(__('dberp.fields.remark'))->limit(40),
            ])
            ->paginated([10, 25, 50]);
    }
}
