<?php

declare(strict_types=1);

namespace App\Business\Settlement;

use App\Models\DberpSettlementEntry;
use Illuminate\Database\Eloquent\Model;

final class SettlementEntryService
{
    public function __construct(
        private readonly SettlementSourceTypeResolver $sourceTypeResolver,
    ) {}

    public function record(
        string $counterpartyType,
        int $counterpartyId,
        string $settlementSide,
        string $changeType,
        string $changeAmount,
        string $balanceAfter,
        ?Model $source = null,
        ?string $documentNumber = null,
        ?string $remark = null,
        mixed $businessTime = null,
    ): void {
        DberpSettlementEntry::query()->create([
            'counterparty_type' => $counterpartyType,
            'counterparty_id' => $counterpartyId,
            'settlement_side' => $settlementSide,
            'change_type' => $changeType,
            'source_type' => $this->sourceTypeResolver->fromModel($source),
            'source_id' => $source?->getKey(),
            'document_number' => $documentNumber,
            'change_amount' => $changeAmount,
            'balance_after' => $balanceAfter,
            'business_time' => $businessTime ?? now(),
            'remark' => $remark,
        ]);
    }
}
