<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpSettlementWriteOffs\RelationManagers;

use App\Business\Settlement\Enums\SettlementWriteOffLineStatus;
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 LinesRelationManager extends RelationManager
{
    protected static string $relationship = 'lines';

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

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

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

    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('receiptOrder.code')
                    ->label(__('dberp.fields.receipt_code')),
                TextColumn::make('paymentOrder.code')
                    ->label(__('dberp.fields.payment_code')),
                TextColumn::make('receivableRecord.code')
                    ->label(__('dberp.fields.receivable_record_code')),
                TextColumn::make('payableRecord.code')
                    ->label(__('dberp.fields.payable_record_code')),
                TextColumn::make('write_off_amount')
                    ->label(__('dberp.fields.write_off_amount'))
                    ->formatStateUsing(fn ($state): string => DberpNumberFormatter::money($state)),
                TextColumn::make('effective_amount')
                    ->label(__('dberp.fields.effective_amount'))
                    ->formatStateUsing(fn ($state): string => DberpNumberFormatter::money($state)),
                TextColumn::make('line_status')
                    ->label(__('dberp.fields.status'))
                    ->badge()
                    ->formatStateUsing(fn (?string $state): string => SettlementWriteOffLineStatus::tryFrom((string) $state)?->label() ?? (string) $state),
                TextColumn::make('business_date')
                    ->label(__('dberp.fields.business_time'))
                    ->dateTime(),
            ])
            ->headerActions([])
            ->recordActions([]);
    }
}
