#!/usr/bin/env python3
"""Exact parametric rebuild of Window 20x60mm body (no keystone/DC cutouts yet)."""
from build123d import *
import os

OUT = "/app/projects/esp32-box/exports"
os.makedirs(OUT, exist_ok=True)

# ========= PARAMETERS (centered at origin) =========
W = 33.9    # X total
H = 13.9    # Y total
T = 4.8     # Z total

T1_Z = 1.6  # Base thickness
T2_Z = 3.2  # Intermediate tier top
T3_Z = 4.8  # Top tier

# Tier 2 (Z=1.6-3.2): has a central gap
# Left strip: X=-16.5 to X=-10.8 = 5.7mm wide
# Right strip: X=+11.5 to X=+16.5 = 5.0mm wide
# Gap in middle: X=-10.8 to X=+11.5 = 22.3mm wide
T2_LEFT_W = 5.7
T2_RIGHT_W = 5.0
T2_Y = 8.0  # Y span

# Tier 3 (Z=3.2-4.8): only left side
T3_W = 14.9
T3_H = 9.9
T3_X = -16.5 + (14.9/2)  # Centered on left side

# ========= BUILD =========

# Tier 1: Full base
base = extrude(Rectangle(W, H), T1_Z)

# Tier 2: Left and right strips at Z=1.6-3.2
t2_left = extrude(Rectangle(T2_LEFT_W, T2_Y), T2_Z - T1_Z)
t2_left = Pos(-16.5 + T2_LEFT_W/2, 0, T1_Z) * t2_left

t2_right = extrude(Rectangle(T2_RIGHT_W, T2_Y), T2_Z - T1_Z)
t2_right = Pos(16.5 - T2_RIGHT_W/2, 0, T1_Z) * t2_right

# Tier 3: Left block at Z=3.2-4.8
t3 = extrude(Rectangle(T3_W, T3_H), T3_Z - T2_Z)
t3 = Pos(T3_X, 0, T2_Z) * t3

# Combine all
body = base + t2_left + t2_right + t3

# Add fillets for the "curves" - smooth transitions between tiers
# Select edges at each transition height
try:
    # Round edges at Z=1.6 transition
    e1 = body.edges().filter_by_position(Axis.Z, T1_Z-0.1, T1_Z+0.1)
    body = fillet(e1, 0.3)
except:
    pass

try:
    # Round edges at Z=3.2 transition
    e2 = body.edges().filter_by_position(Axis.Z, T2_Z-0.1, T2_Z+0.1)
    body = fillet(e2, 0.3)
except:
    pass

# Export
step_path = os.path.join(OUT, "window_body.stp")
export_step(body, step_path)
print("STEP: %s" % step_path)
print("Dim: %.1f x %.1f x %.1f" % (body.bounding_box().size.X, body.bounding_box().size.Y, body.bounding_box().size.Z))
