<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpSettlementAdjustments\Pages;

use App\Business\Settlement\Enums\SettlementAdjustmentStatus;
use App\Filament\Resources\DberpSettlementAdjustments\DberpSettlementAdjustmentResource;
use App\Filament\Resources\Pages\CreateRecord;

class CreateDberpSettlementAdjustment extends CreateRecord
{
    protected static string $resource = DberpSettlementAdjustmentResource::class;

    /**
     * @param  array<string, mixed>  $data
     * @return array<string, mixed>
     */
    protected function mutateFormDataBeforeCreate(array $data): array
    {
        $data['counterparty_type'] = ($data['settlement_side'] ?? 'receivable') === 'payable' ? 'supplier' : 'customer';
        $data['status'] = SettlementAdjustmentStatus::Draft->value;
        $data['effective_amount'] = '0.0000';
        $data['source_system'] = 'dberp';

        if (($data['settlement_side'] ?? 'receivable') === 'payable') {
            $data['receivable_record_id'] = null;
        } else {
            $data['payable_record_id'] = null;
        }

        return $data;
    }
}