"""OKX_TRADE_MODE 三选一."""

import os
import unittest
from unittest.mock import patch

from lib.hedge_plan.okx_trade_mode_lib import (
    MODE_OO,
    MODE_OPTIONS,
    MODE_PERP,
    block_standalone_open_by_mode_msg,
    get_okx_trade_mode,
    hedge_module_enabled,
    show_options_options,
    show_perp_options,
    standalone_options_open_allowed,
)


class OkxTradeModeTests(unittest.TestCase):
    def test_explicit_modes(self):
        for mode, hedge, po, oo in (
            (MODE_OPTIONS, False, False, False),
            (MODE_PERP, True, True, False),
            (MODE_OO, True, False, True),
        ):
            with patch.dict(os.environ, {"OKX_TRADE_MODE": mode}, clear=False):
                self.assertEqual(get_okx_trade_mode(), mode)
                self.assertEqual(hedge_module_enabled(), hedge)
                self.assertEqual(show_perp_options(), po)
                self.assertEqual(show_options_options(), oo)
                self.assertEqual(standalone_options_open_allowed(), mode == MODE_OPTIONS)

    def test_legacy_infer_options(self):
        with patch.dict(
            os.environ,
            {"HEDGE_PLAN_ENABLED": "false"},
            clear=False,
        ):
            os.environ.pop("OKX_TRADE_MODE", None)
            self.assertEqual(get_okx_trade_mode(), MODE_OPTIONS)

    def test_legacy_infer_perp(self):
        with patch.dict(
            os.environ,
            {
                "HEDGE_PLAN_ENABLED": "true",
                "HEDGE_PLAN_SHOW_PERP_OPTIONS": "true",
                "HEDGE_PLAN_SHOW_OPTIONS_OPTIONS": "false",
            },
            clear=False,
        ):
            os.environ.pop("OKX_TRADE_MODE", None)
            self.assertEqual(get_okx_trade_mode(), MODE_PERP)

    def test_block_standalone_in_hedge_mode(self):
        with patch.dict(os.environ, {"OKX_TRADE_MODE": MODE_OO}):
            msg = block_standalone_open_by_mode_msg()
            self.assertIsNotNone(msg)
            self.assertIn("期期", msg or "")


class EnvUiModeSectionsTests(unittest.TestCase):
    def test_options_mode_hides_hedge_section(self):
        from lib.env.env_ui_manifest import ui_sections_for_exchange

        titles = [s["title"] for s in ui_sections_for_exchange("okx", mode="options")]
        self.assertIn("期权/对冲模式", titles)
        self.assertIn("期权账户", titles)
        self.assertTrue(all("对冲" not in t for t in titles if t != "期权/对冲模式"))

    def test_perp_mode_shows_po_fields(self):
        from lib.env.env_ui_manifest import ui_sections_for_exchange

        secs = {s["title"]: s["fields"] for s in ui_sections_for_exchange("okx", mode="perp_options")}
        self.assertIn("对冲计划·永期", secs)
        keys = {f[0] for f in secs["对冲计划·永期"]}
        self.assertIn("MAX_ACTIVE_HEDGE_PLANS", keys)
        self.assertIn("HEDGE_PLAN_OPEN_ORDER", keys)
        self.assertNotIn("HEDGE_PLAN_OO_BIAS_RATIO", keys)
        opt_keys = {f[0] for f in secs["期权账户"]}
        self.assertNotIn("OKX_OPTIONS_MAX_ACTIVE_POSITIONS", opt_keys)

    def test_oo_mode_shows_oo_fields(self):
        from lib.env.env_ui_manifest import ui_sections_for_exchange

        secs = {s["title"]: s["fields"] for s in ui_sections_for_exchange("okx", mode="options_options")}
        self.assertIn("对冲计划·期期", secs)
        keys = {f[0] for f in secs["对冲计划·期期"]}
        self.assertIn("HEDGE_PLAN_OO_BIAS_RATIO", keys)
        self.assertNotIn("HEDGE_PLAN_OPEN_ORDER", keys)


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