<?php

declare(strict_types=1);

namespace App\Business\Settlement;

use App\Models\DberpDictionaryType;
use App\Models\DberpSystemProfile;

final class SettlementRuleService
{
    public function profile(): DberpSystemProfile
    {
        return DberpSystemProfile::query()->firstOrCreate(
            ['id' => 1],
            DberpSystemProfile::defaultAttributes(),
        );
    }

    public function receiptOverLimitPolicy(): string
    {
        return (string) $this->profile()->receipt_over_limit_policy;
    }

    public function paymentOverLimitPolicy(): string
    {
        return (string) $this->profile()->payment_over_limit_policy;
    }

    public function allowReceiptOverLimit(): bool
    {
        return $this->receiptOverLimitPolicy() !== 'forbid';
    }

    public function allowPaymentOverLimit(): bool
    {
        return $this->paymentOverLimitPolicy() !== 'forbid';
    }

    public function allowSettlementOnClosedDocument(): bool
    {
        return (bool) $this->profile()->allow_settlement_on_closed_document;
    }

    public function settlementVoidPolicy(): string
    {
        return (string) $this->profile()->settlement_void_policy;
    }

    public function allowsSettlementVoid(): bool
    {
        return $this->settlementVoidPolicy() !== 'forbid';
    }

    public function autoRollbackSettlementVoid(): bool
    {
        return $this->settlementVoidPolicy() === 'rollback';
    }

    public function receiptMethodTypeId(): ?int
    {
        return DberpDictionaryType::query()->where('code', 'receipt_method')->value('id');
    }

    public function paymentMethodTypeId(): ?int
    {
        return DberpDictionaryType::query()->where('code', 'payment_method')->value('id');
    }
}
