import sys
import os

# Add project root to sys.path
sys.path.insert(0, r"c:\Projects\FreeExile")
sys.path.insert(0, r"c:\Projects\FreeExile\.agents\teamwork\explorer_m2_progression_1_gen2")

# Import the proposed class
import proposed_level_progression_service as proposed_mod
LevelProgressionService = proposed_mod.LevelProgressionService

s = LevelProgressionService()

# 1. Check get_canonical_benchmark
b100 = s.get_canonical_benchmark(100)
assert b100.cumulative_exp == 24285477315, f"b100.cumulative_exp mismatch: {b100.cumulative_exp}"
print(f"[TEST 1 PASS] b100.cumulative_exp == {b100.cumulative_exp}")

# 2. Check Level 100 Cumulative EXP Clamp (Zero Overflow)
s.set_player_state('test_overflow', level=99, current_exp=s.get_delta_exp(99) - 10)
award_res = s.award_monster_exp('test_overflow', 100, 100, 1000000)
info = s.get_level_info('test_overflow')
diff = info.cumulative_exp - b100.cumulative_exp
assert diff == 0, f"Overflow detected: {diff}"
assert info.cumulative_exp == 24285477315, f"Expected 24285477315, got {info.cumulative_exp}"
assert info.exp_to_next_level == 0, f"Expected exp_to_next_level == 0, got {info.exp_to_next_level}"
assert info.current_exp == 0, f"Expected current_exp == 0, got {info.current_exp}"
assert award_res.new_level == 100
assert award_res.current_exp == 0
assert award_res.exp_to_next_level == 0
print("[TEST 2 PASS] Level 100 cumulative EXP strictly clamped with ZERO overflow")

# 3. Check Level 100 Death Listener & Counter
s.set_player_state('test_death_lv100', level=100)
events = []
s.add_death_penalty_listener(lambda r: events.append(r))
death_res = s.apply_death_penalty('test_death_lv100')
info_death = s.get_level_info('test_death_lv100')
assert info_death.deaths_count == 1, f"Expected deaths_count == 1, got {info_death.deaths_count}"
assert len(events) == 1, f"Expected 1 event, got {len(events)}"
assert death_res.exp_lost == 0, f"Expected exp_lost == 0, got {death_res.exp_lost}"
assert death_res.penalty_ratio == 0.0, f"Expected penalty_ratio == 0.0, got {death_res.penalty_ratio}"
assert death_res.current_exp_after == 0
assert death_res.de_leveled is False
print("[TEST 3 PASS] Level 100 death listener notified and deaths_count incremented")

# 4. Check Negative Base EXP and Raw EXP Sanitization
s.set_player_state('test_neg_exp', level=10, current_exp=100)
res_neg1 = s.award_monster_exp('test_neg_exp', 10, base_exp=-100)
assert res_neg1.raw_exp == 0, f"Expected raw_exp == 0, got {res_neg1.raw_exp}"
assert res_neg1.exp_awarded == 0, f"Expected exp_awarded == 0, got {res_neg1.exp_awarded}"
assert s.get_level_info('test_neg_exp').current_exp == 100, f"EXP changed: {s.get_level_info('test_neg_exp').current_exp}"

res_neg2 = s.award_monster_exp('test_neg_exp', 10, 10, -50)
assert res_neg2.raw_exp == 0
assert res_neg2.exp_awarded == 0
assert s.get_level_info('test_neg_exp').current_exp == 100

print("[TEST 4 PASS] Negative EXP sanitized to 0")

# 5. Check Line Count and Function Length
file_path = r"c:\Projects\FreeExile\.agents\teamwork\explorer_m2_progression_1_gen2\proposed_level_progression_service.py"
with open(file_path, "r", encoding="utf-8") as f:
    lines = f.readlines()
total_lines = len(lines)
assert total_lines <= 350, f"Total lines {total_lines} exceeds soft cap 350!"
assert total_lines <= 500, f"Total lines {total_lines} exceeds hard cap 500!"
print(f"[TEST 5 PASS] Total lines = {total_lines} <= 350 (Soft Cap)")

# 6. AST Check for function length <= 50 lines
import ast
with open(file_path, "r", encoding="utf-8") as f:
    tree = ast.parse(f.read(), filename=file_path)

for node in ast.walk(tree):
    if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
        length = node.end_lineno - node.lineno + 1
        assert length <= 50, f"Function {node.name} length {length} > 50 lines!"
        print(f"  Function {node.name}: {length} lines (OK)")

print("[TEST 6 PASS] All functions <= 50 lines (AST Check)")

print("\nALL 6 CHECKS PASSED PERFECTLY!")
