<?php

namespace App\Services\Wallet;

use App\Models\Player;

/**
 * 未配置 `MAIN_SITE_WALLET_API_URL` 或仅本地联调时使用：主站永远返回成功。
 */
final class StubMainSiteWalletGateway implements MainSiteWalletGateway
{
    public function debitMainForLotteryDeposit(
        Player $player,
        string $currencyCode,
        int $amountMinor,
        string $idempotentKey,
    ): MainSiteWalletResult {
        $req = self::requestSnapshot($player, $currencyCode, $amountMinor, $idempotentKey, 'stub_debit');

        return MainSiteWalletResult::success('stub-debit:'.$idempotentKey, [
            'stub' => true,
            'currency' => $currencyCode,
            'amount_minor' => $amountMinor,
        ], $req);
    }

    public function creditMainForLotteryWithdraw(
        Player $player,
        string $currencyCode,
        int $amountMinor,
        string $idempotentKey,
    ): MainSiteWalletResult {
        $req = self::requestSnapshot($player, $currencyCode, $amountMinor, $idempotentKey, 'stub_credit');

        return MainSiteWalletResult::success('stub-credit:'.$idempotentKey, [
            'stub' => true,
            'currency' => $currencyCode,
            'amount_minor' => $amountMinor,
        ], $req);
    }

    public function refundMainForFailedLotteryDeposit(
        Player $player,
        string $currencyCode,
        int $amountMinor,
        string $idempotentKey,
    ): MainSiteWalletResult {
        $req = self::requestSnapshot($player, $currencyCode, $amountMinor, $idempotentKey, 'stub_refund');

        return MainSiteWalletResult::success('stub-refund:'.$idempotentKey, [
            'stub' => true,
            'currency' => $currencyCode,
            'amount_minor' => $amountMinor,
        ], $req);
    }

    /**
     * @return array<string, mixed>
     */
    private static function requestSnapshot(
        Player $player,
        string $currencyCode,
        int $amountMinor,
        string $idempotentKey,
        string $stubOp,
    ): array {
        return [
            'site_code' => $player->site_code,
            'site_player_id' => $player->site_player_id,
            'player_id' => $player->id,
            'currency_code' => $currencyCode,
            'amount_minor' => $amountMinor,
            'idempotent_key' => $idempotentKey,
            '_meta' => [
                'stub' => true,
                'operation' => $stubOp,
            ],
        ];
    }
}
