"""
Unit tests for FreeExile Currency Exchange & Purchasing Power Parity (PPP) Engine.
Validates multi-currency fiat pricing, regional PPP tiers, Web3/Web direct bonus coins,
and anti-region-arbitrage safeguards.
"""

import unittest
import sys
import os

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../server")))

from shop.currency_exchange import (
    CurrencyExchangeEngine,
    RegionalPricingTier,
    FiatCurrency,
)


class TestCurrencyExchange(unittest.TestCase):
    def setUp(self):
        self.fx = CurrencyExchangeEngine()

    def test_fiat_to_coin_conversion_base(self):
        """Standard USD tier: 1 USD = 100 in-game Blood Coins (Huyết Cổ Tệ)."""
        coins = self.fx.convert_usd_to_coins(usd_amount=10.0)
        self.assertEqual(coins, 1000)

    def test_regional_ppp_multipliers(self):
        """
        Purchasing Power Parity (PPP):
        - US, JP: Tier 1 (PPP = 1.0)
        - KR: Tier 2 (PPP ~ 0.90)
        - VN: Tier 3 (PPP ~ 0.50, localized purchasing power)
        """
        tier_us = self.fx.get_regional_tier("US")
        tier_vn = self.fx.get_regional_tier("VN")
        tier_kr = self.fx.get_regional_tier("KR")

        self.assertEqual(tier_us, RegionalPricingTier.TIER_1_STANDARD)
        self.assertEqual(tier_kr, RegionalPricingTier.TIER_2_MODERATE)
        self.assertEqual(tier_vn, RegionalPricingTier.TIER_3_EMERGING)

        # Localized localized fiat price for a standard $10 item
        usd_price = 10.0
        vnd_price = self.fx.calculate_localized_fiat_price(usd_price, "VN")
        # Base: $10 * 25,000 VND/USD * 0.50 PPP factor = ~125,000 VND
        self.assertAlmostEqual(vnd_price, 125000.0, delta=5000.0)

    def test_direct_web_and_crypto_bonus(self):
        """Purchases on Web Direct Store or Web3 Crypto receive +10% bonus coins."""
        coins_ios = self.fx.calculate_coins_reward(
            base_usd=10.0,
            platform="PLATFORM_IOS",
            is_web3_crypto=False
        )
        coins_web_crypto = self.fx.calculate_coins_reward(
            base_usd=10.0,
            platform="PLATFORM_WEB_PC",
            is_web3_crypto=True
        )
        self.assertEqual(coins_ios, 1000)
        self.assertEqual(coins_web_crypto, 1100) # +10% bonus for 0% App Store cut

    def test_anti_region_arbitrage_detection(self):
        """Discrepancy between IP location and Payment Method Issuer Country triggers warning/reject."""
        # Genuine user from Vietnam with VN payment method
        valid, reason = self.fx.validate_billing_region(
            ip_country="VN",
            payment_country="VN"
        )
        self.assertTrue(valid)

        # VPN user: US IP attempting to pay with VN regional subsidized rate
        valid_vpn, reason = self.fx.validate_billing_region(
            ip_country="US",
            payment_country="VN"
        )
        self.assertFalse(valid_vpn)
        self.assertIn("REGION_MISMATCH", reason)


if __name__ == "__main__":
    unittest.main()
