<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpSettlementStatements\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 ItemsRelationManager extends RelationManager
{
    protected static string $relationship = 'items';

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

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

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

    public function form(Schema $schema): Schema
    {
        return $schema->components([]);
    }

    public function table(Table $table): Table
    {
        return $table
            ->defaultSort('line_no', 'asc')
            ->columns([
                TextColumn::make('line_no')
                    ->label(__('dberp.fields.line_no')),
                TextColumn::make('business_time')
                    ->label(__('dberp.fields.business_time'))
                    ->dateTime(),
                TextColumn::make('source_type')
                    ->label(__('dberp.fields.source_type'))
                    ->formatStateUsing(fn (?string $state): string => __('dberp.options.settlement_source_type')[$state] ?? (string) $state),
                TextColumn::make('source_document_no')
                    ->label(__('dberp.fields.document_code')),
                TextColumn::make('summary')
                    ->label(__('dberp.fields.summary')),
                TextColumn::make('debit_amount')
                    ->label(__('dberp.fields.debit_amount'))
                    ->formatStateUsing(fn ($state): string => DberpNumberFormatter::money($state)),
                TextColumn::make('credit_amount')
                    ->label(__('dberp.fields.credit_amount'))
                    ->formatStateUsing(fn ($state): string => DberpNumberFormatter::money($state)),
                TextColumn::make('net_change_amount')
                    ->label(__('dberp.fields.net_change_amount'))
                    ->formatStateUsing(fn ($state): string => DberpNumberFormatter::money($state)),
                TextColumn::make('balance_after')
                    ->label(__('dberp.fields.balance_after'))
                    ->formatStateUsing(fn ($state): string => DberpNumberFormatter::money($state)),
            ])
            ->headerActions([])
            ->recordActions([]);
    }
}
