<?php

use App\Services\AgentSettlement\ShareSettlementCalculator;
use App\Support\Settlement\DesignDocExample12;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

test('design doc example 12 actual share rates sum to 100 percent', function (): void {
    $rates = DesignDocExample12::actualShareRates();
    expect($rates['total'])->toEqual(100.0);
    expect($rates['actual']['C'])->toEqual(25.0);
    expect($rates['actual']['B'])->toEqual(15.0);
    expect($rates['actual']['A'])->toEqual(20.0);
    expect($rates['actual']['platform'])->toEqual(40.0);
});

test('design doc example 12 share settlement matches section 12.3 and 12.4', function (): void {
    $calculator = new ShareSettlementCalculator;
    $result = $calculator->calculate(
        sharedNetWinLoss: DesignDocExample12::SHARED_NET_WIN_LOSS,
        totalSharesByCode: [
            'A' => DesignDocExample12::TOTAL_SHARE_A,
            'B' => DesignDocExample12::TOTAL_SHARE_B,
            'C' => DesignDocExample12::TOTAL_SHARE_C,
        ],
        extraRebateByCode: ['C' => DesignDocExample12::EXTRA_REBATE_BY_C],
        gameWinLoss: DesignDocExample12::GAME_WIN_LOSS,
        basicRebate: DesignDocExample12::BASIC_REBATE,
        chainFromPlayer: ['C', 'B', 'A'],
    );

    expect($result->playerNetSettlement)->toEqual((float) DesignDocExample12::PLAYER_NET_SETTLEMENT);
    expect($result->sharedNetWinLoss)->toEqual((float) DesignDocExample12::SHARED_NET_WIN_LOSS);
    expect($result->shareProfits['C'])->toEqual(DesignDocExample12::SHARE_PROFIT_C);
    expect($result->shareProfits['B'])->toEqual(DesignDocExample12::SHARE_PROFIT_B);
    expect($result->shareProfits['A'])->toEqual(DesignDocExample12::SHARE_PROFIT_A);
    expect($result->shareProfits['platform'])->toEqual(DesignDocExample12::SHARE_PROFIT_PLATFORM);
    expect($result->finalProfits['C'])->toEqual(DesignDocExample12::FINAL_PROFIT_C);
    expect($result->tierSettlements['P_to_C'])->toEqual(DesignDocExample12::TIER_P_TO_C);
    expect($result->tierSettlements['C_to_B'])->toEqual(DesignDocExample12::TIER_C_TO_B);
    expect($result->tierSettlements['B_to_A'])->toEqual(DesignDocExample12::TIER_B_TO_A);
    expect($result->tierSettlements['A_to_platform'])->toEqual(DesignDocExample12::TIER_A_TO_PLATFORM);
});
