<?php

namespace App\Services\Settlement;

use App\Models\TicketItem;
use App\Services\LotterySettings;

/**
 * 派彩侧「回水再扣」开关（默认关：实扣已在下注阶段处理；与 PRD 一致时可打开）。
 */
final class SettlementPayoutAdjuster
{
    public function adjustGrossWin(int $grossWin, TicketItem $item): int
    {
        if ($grossWin <= 0) {
            return 0;
        }

        if (! (bool) LotterySettings::get('settlement.apply_rebate_to_payout', false)) {
            return $grossWin;
        }

        $rebate = (float) $item->rebate_rate_snapshot;

        return (int) floor($grossWin * max(0.0, 1.0 - $rebate));
    }
}
