"""
Unit tests for FreeExile Platform Payment Compliance Router.
Enforces Apple App Store Guideline 3.1.1 and Google Play Billing requirements.
Guarantees iOS builds NEVER expose 3rd party gateways, while Web/PC can offer Crypto & Cards.
"""

import unittest
import sys
import os

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

from shop.payment_gateway_router import (
    PaymentGatewayRouter,
    PlatformType,
    PaymentGateway,
    PlatformPolicyViolationError,
)


class TestPaymentCompliance(unittest.TestCase):
    def setUp(self):
        self.router = PaymentGatewayRouter()

    def test_ios_strictly_limits_to_apple_iap(self):
        """
        Apple App Store Guideline 3.1.1:
        iOS builds MUST ONLY offer Apple In-App Purchase (StoreKit 2).
        Third-party processors (Stripe, PayPal, Crypto, MoMo) are STRICTLY FORBIDDEN.
        """
        methods = self.router.get_allowed_payment_methods(
            platform=PlatformType.PLATFORM_IOS,
            country_code="US"
        )
        gateways = [m.gateway for m in methods]

        self.assertEqual(len(gateways), 1)
        self.assertEqual(gateways[0], PaymentGateway.GATEWAY_APPLE_IAP)
        self.assertNotIn(PaymentGateway.GATEWAY_STRIPE, gateways)
        self.assertNotIn(PaymentGateway.GATEWAY_CRYPTO_WEB3, gateways)

    def test_ios_rejects_third_party_order_creation(self):
        """Attempting to initiate a Stripe or Web3 Crypto order from an iOS platform throws PolicyViolation."""
        with self.assertRaises(PlatformPolicyViolationError):
            self.router.create_payment_order(
                account_id="acc_ios_user_01",
                product_id="coin_pack_1000",
                platform=PlatformType.PLATFORM_IOS,
                requested_gateway=PaymentGateway.GATEWAY_CRYPTO_WEB3,
                country_code="VN"
            )

        with self.assertRaises(PlatformPolicyViolationError):
            self.router.create_payment_order(
                account_id="acc_ios_user_02",
                product_id="coin_pack_1000",
                platform=PlatformType.PLATFORM_IOS,
                requested_gateway=PaymentGateway.GATEWAY_STRIPE,
                country_code="US"
            )

    def test_android_playstore_limits_to_google_play_billing(self):
        """Standard Google Play builds must only expose Google Play Billing."""
        methods = self.router.get_allowed_payment_methods(
            platform=PlatformType.PLATFORM_ANDROID_PLAYSTORE,
            country_code="US"
        )
        gateways = [m.gateway for m in methods]
        self.assertEqual(len(gateways), 1)
        self.assertEqual(gateways[0], PaymentGateway.GATEWAY_GOOGLE_PLAY)

    def test_web_pc_and_sideload_allow_full_gateways_including_crypto(self):
        """
        Official Web Direct Portal and APK sideload allow full gateway suite:
        Stripe, PayPal, Local QR, and Web3 Crypto.
        """
        web_methods = self.router.get_allowed_payment_methods(
            platform=PlatformType.PLATFORM_WEB_PC,
            country_code="VN"
        )
        web_gateways = {m.gateway for m in web_methods}

        self.assertIn(PaymentGateway.GATEWAY_CRYPTO_WEB3, web_gateways)
        self.assertIn(PaymentGateway.GATEWAY_STRIPE, web_gateways)
        self.assertNotIn(PaymentGateway.GATEWAY_APPLE_IAP, web_gateways)


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