"""
Parity test for score_thresholds_v1.py against GAS source.

F07, F01, F09: Score calculation thresholds.
Method: Extract THRESHOLDS object from GAS, compare values against Python constants.

Source: src/gas_adapter_parts/gdf_01_price_metrics.gs lines 260-304
Key values:
  - F07: SP_TAKE_PROFIT = 10 (used in line 1702: score += THRESHOLDS["SP_TAKE_PROFIT"])
  - F01: SP_TAKE_PROFIT = 10 (already registered in spec/calibration_registry.yaml)
  - F09: TAKE_PROFIT_BASE = 10 (already registered)
"""

import pytest
from formulas.score_thresholds_v1 import (
    SP_TAKE_PROFIT,
    SP_HOLDINGS_ROTATE,
    SP_SELL_SIGNAL,
    TP_CORE_1,
    TP_CORE_2,
    TP_SAT_1,
    TP_SAT_2,
    TAKE_PROFIT_BASE,
    TIME_STOP_STAGE1,
    TIME_STOP_STAGE2,
    VAL_SURGE_WATCH,
    VAL_SURGE_HOT,
    VAL_SURGE_EXHAUSTED,
    LIQUIDITY_PREFERRED_M,
    LIQUIDITY_OK_M,
    SPREAD_OK_PCT,
    SPREAD_WARN_PCT,
    get_threshold,
)


class TestScoreThresholdsParity:
    """Verify all threshold constants match GAS THRESHOLDS object exactly"""

    def test_exit_scoring_thresholds_match_gas(self):
        """Exit signal thresholds must match GAS lines 302-304"""
        assert SP_TAKE_PROFIT == 10, "F07: score += THRESHOLDS['SP_TAKE_PROFIT']"
        assert SP_HOLDINGS_ROTATE == 20, "EXIT_REVIEW signal threshold"
        assert SP_SELL_SIGNAL == 40, "SELL_READY / TRIM signal threshold"

    def test_profit_taking_multipliers_match_gas(self):
        """Take-profit tier multipliers must match GAS lines 271-275"""
        assert TP_CORE_1 == 1.15, "Core 1st tier: +15% from entry"
        assert TP_CORE_2 == 1.25, "Core 2nd tier: +25% from entry"
        assert TP_SAT_1 == 1.10, "Satellite 1st tier: +10% from entry"
        assert TP_SAT_2 == 1.20, "Satellite 2nd tier: +20% from entry"
        assert TAKE_PROFIT_BASE == 10, "F09: Base take-profit percentage"

    def test_time_stop_thresholds_match_gas(self):
        """Time stop calendar day thresholds must match GAS lines 276-278"""
        assert TIME_STOP_STAGE1 == 60, "60-day time stop stage 1"
        assert TIME_STOP_STAGE2 == 30, "30-day time stop stage 2"

    def test_val_surge_thresholds_match_gas(self):
        """Value surge percentage thresholds must match GAS lines 261-264"""
        assert VAL_SURGE_WATCH == 15, "Watch threshold for value surge"
        assert VAL_SURGE_HOT == 35, "Hot threshold for value surge"
        assert VAL_SURGE_EXHAUSTED == 50, "Exhausted threshold for value surge"

    def test_liquidity_thresholds_match_gas(self):
        """Liquidity thresholds (5D avg trading value in millions KRW) must match GAS lines 265-267"""
        assert LIQUIDITY_PREFERRED_M == 100, "Preferred liquidity threshold (millions KRW)"
        assert LIQUIDITY_OK_M == 50, "Acceptable liquidity threshold (millions KRW)"

    def test_spread_thresholds_match_gas(self):
        """Bid-ask spread thresholds (%) must match GAS lines 268-270"""
        assert SPREAD_OK_PCT == 0.25, "Acceptable spread: 0.25%"
        assert SPREAD_WARN_PCT == 0.50, "Warning spread: 0.50%"


class TestGetThresholdFunction:
    """get_threshold() function provides GAS THRESHOLDS[key] compatibility"""

    def test_get_threshold_returns_correct_values(self):
        """get_threshold() should return the same value as direct constant access"""
        assert get_threshold('SP_TAKE_PROFIT') == SP_TAKE_PROFIT
        assert get_threshold('SP_HOLDINGS_ROTATE') == SP_HOLDINGS_ROTATE
        assert get_threshold('SP_SELL_SIGNAL') == SP_SELL_SIGNAL
        assert get_threshold('TP_CORE_1') == TP_CORE_1
        assert get_threshold('TAKE_PROFIT_BASE') == TAKE_PROFIT_BASE

    def test_get_threshold_supports_gas_access_pattern(self):
        """Mimics GAS THRESHOLDS["SP_TAKE_PROFIT"] access pattern"""
        # GAS: score += THRESHOLDS["SP_TAKE_PROFIT"]
        # Python: score += get_threshold("SP_TAKE_PROFIT")
        sp_take_profit_value = get_threshold("SP_TAKE_PROFIT")
        assert sp_take_profit_value == 10

    def test_get_threshold_returns_none_for_unknown_key(self):
        """Unknown keys return None (graceful fallback)"""
        assert get_threshold('UNKNOWN_KEY') is None
