"""
Mathematical verification script for UV overflow fix strategies.
Runs against the actual data from MONSTER_ARCHETYPES and EXILE_CHARACTERS in monster_character_pipeline_scaffold.py.
"""
import math

CANONICAL_DIRECTIONS = ["S", "SW", "W", "NW", "N", "NE", "E", "SE"]

DEFAULT_MONSTER_ACTIONS = {
    "idle": {"frameCount": 4, "fps": 6, "loop": True},
    "walk": {"frameCount": 6, "fps": 10, "loop": True},
    "attack": {"frameCount": 6, "fps": 14, "loop": False, "hit_frame": 2},
    "hurt": {"frameCount": 3, "fps": 12, "loop": False},
    "death": {"frameCount": 5, "fps": 10, "loop": False},
    "stun": {"frameCount": 3, "fps": 8, "loop": True},
}

DEFAULT_EXILE_ACTIONS = {
    "idle": {"frameCount": 4, "fps": 6, "loop": True},
    "run": {"frameCount": 8, "fps": 12, "loop": True},
    "attack_slash": {"frameCount": 6, "fps": 15, "loop": False, "hit_frame": 2},
    "attack_thrust": {"frameCount": 6, "fps": 15, "loop": False, "hit_frame": 2},
    "attack_slam": {"frameCount": 6, "fps": 15, "loop": False, "hit_frame": 3},
    "attack_shoot": {"frameCount": 6, "fps": 15, "loop": False, "hit_frame": 2},
    "skill_whirlwind": {"frameCount": 6, "fps": 18, "loop": True},
    "dodge": {"frameCount": 6, "fps": 20, "loop": False, "iframe_duration_ms": 250},
    "hurt": {"frameCount": 3, "fps": 12, "loop": False},
    "death": {"frameCount": 5, "fps": 10, "loop": False},
}

def next_power_of_two(n: int) -> int:
    return 1 << (n - 1).bit_length()

print("=== MONSTER ARCHETYPES MATHEMATICAL VERIFICATION ===")
m_fw, m_fh = 160, 160
m_clips = len(DEFAULT_MONSTER_ACTIONS) * len(CANONICAL_DIRECTIONS)
m_total_rows = m_clips
m_total_h = m_total_rows * m_fh
m_total_w = 8 * m_fw
m_pot_w = next_power_of_two(m_total_w)
m_pot_h = next_power_of_two(m_total_h)

print(f"Monster total clips: {m_clips}")
print(f"Required height: {m_total_h}px across {m_total_rows} rows")
print(f"Required width: {m_total_w}px across 8 cols")
print(f"Calculated PoT texture: {m_pot_w} x {m_pot_h}")

m_max_u1 = (max(act['frameCount'] for act in DEFAULT_MONSTER_ACTIONS.values()) * m_fw) / float(m_pot_w)
m_max_v1 = m_total_h / float(m_pot_h)
print(f"Monster Max U1: {m_max_u1:.6f} <= 1.0? {m_max_u1 <= 1.0}")
print(f"Monster Max V1: {m_max_v1:.6f} <= 1.0? {m_max_v1 <= 1.0}")

print("\n=== EXILE CHARACTERS MATHEMATICAL VERIFICATION ===")
c_fw, c_fh = 160, 192
c_clips = len(DEFAULT_EXILE_ACTIONS) * len(CANONICAL_DIRECTIONS)
c_total_rows = c_clips
c_total_h = c_total_rows * c_fh
c_total_w = 8 * c_fw
c_pot_w = next_power_of_two(c_total_w)
c_pot_h = next_power_of_two(c_total_h)

print(f"Character total clips: {c_clips}")
print(f"Required height: {c_total_h}px across {c_total_rows} rows")
print(f"Required width: {c_total_w}px across 8 cols")
print(f"Calculated PoT texture: {c_pot_w} x {c_pot_h}")

c_max_u1 = (max(act['frameCount'] for act in DEFAULT_EXILE_ACTIONS.values()) * c_fw) / float(c_pot_w)
c_max_v1 = c_total_h / float(c_pot_h)
print(f"Character Max U1: {c_max_u1:.6f} <= 1.0? {c_max_u1 <= 1.0}")
print(f"Character Max V1: {c_max_v1:.6f} <= 1.0? {c_max_v1 <= 1.0}")

print("\n=== ALTERNATIVE: CHARACTER 4096 x 8192 (16 cols) ===")
c_alt_w = 4096
c_alt_h = 8192
c_alt_rows = c_clips // 2  # 2 directions per row
c_alt_total_h = c_alt_rows * c_fh
c_alt_max_u1 = (16 * c_fw) / float(c_alt_w)
c_alt_max_v1 = c_alt_total_h / float(c_alt_h)
print(f"Required height: {c_alt_total_h}px across {c_alt_rows} rows")
print(f"Character Alt Max U1: {c_alt_max_u1:.6f} <= 1.0? {c_alt_max_u1 <= 1.0}")
print(f"Character Alt Max V1: {c_alt_max_v1:.6f} <= 1.0? {c_alt_max_v1 <= 1.0}")

print("\n=== AREA COMPARISON WITH 2048x2048 ===")
tex_area = 2048 * 2048
m_total_frames = sum(act['frameCount'] * 8 for act in DEFAULT_MONSTER_ACTIONS.values())
m_frame_area = m_total_frames * m_fw * m_fh
print(f"Monster total frames: {m_total_frames}, Frame pixel area: {m_frame_area:,}")
print(f"2048x2048 Texture area: {tex_area:,}")
print(f"Monster Frame Area / Texture Area: {m_frame_area / tex_area:.4f} (impossible to fit into 1 sheet!)")

c_total_frames = sum(act['frameCount'] * 8 for act in DEFAULT_EXILE_ACTIONS.values())
c_frame_area = c_total_frames * c_fw * c_fh
print(f"Character total frames: {c_total_frames}, Frame pixel area: {c_frame_area:,}")
print(f"Character Frame Area / Texture Area: {c_frame_area / tex_area:.4f} (requires {math.ceil(c_frame_area / tex_area)} sheets of 2048x2048!)")
