<?php

declare(strict_types=1);

namespace ImovelZapAi\Financial\Application\Actions;

use ImovelZapAi\Financial\Domain\Enums\CommissionBasis;
use ImovelZapAi\Financial\Domain\Enums\DealFinanceType;
use ImovelZapAi\Financial\Infrastructure\Persistence\Eloquent\FinancialCommissionRuleModel;

final class EnsureDefaultCommissionRuleAction
{
    public function execute(string $tenantId): FinancialCommissionRuleModel
    {
        $existing = FinancialCommissionRuleModel::query()
            ->where('tenant_id', $tenantId)
            ->where('is_default', true)
            ->where('is_active', true)
            ->first();

        if ($existing !== null) {
            return $existing;
        }

        return FinancialCommissionRuleModel::query()->create([
            'tenant_id' => $tenantId,
            'name' => 'Comissão padrão venda',
            'basis' => CommissionBasis::Percent,
            'value' => 6,
            'deal_type' => DealFinanceType::Sale,
            'split_json' => null,
            'is_default' => true,
            'is_active' => true,
        ]);
    }
}
