<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

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;
    }
}