#!/usr/bin/env python3
"""
Parametric rebuild of Window 20x60mm connection panel.
Based on STL analysis + image analysis:
- 33.9 x 13.9 x 4.8mm
- Base Z=0-1.6 (full rectangle)
- Frame Z=1.6-3.2 (oval cutout + grooves)
- Left collar Z=3.2-4.8
- Central oval opening
- Grooves on top/bottom edges
"""
from build123d import *
import os, math

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

# Dimensions (centered at origin)
W = 33.9
H = 13.9
T = 4.8

# Heights
Z1 = 1.6  # Base top
Z2 = 3.2  # Frame top
Z3 = 4.8  # Collar top

# Oval cutout (frame inner)
OVAL_W = 22.0  # oval width (X)
OVAL_H = 7.0   # oval height (Y)

# Frame outer dimensions at Z2
F_W = 27.5   # Frame outer width (X)
F_H = 8.0    # Frame outer height (Y)
F_X = 0  # centered for now

# Left collar at Z3
C_W = 14.9
C_H = 9.9
C_X = -W/2 + C_W/2  # Left side

# Grooves on top/bottom of frame
N_GROOVES = 8  # Number of grooves along top edge
GROOVE_W = 2.0
GROOVE_D = 0.3  # Depth
GROOVE_GAP = (F_W - N_GROOVES * GROOVE_W) / (N_GROOVES - 1) if N_GROOVES > 1 else 0

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

# 1. Base: full rectangle Z=0-1.6
base = extrude(Rectangle(W, H), Z1)

# 2. Outer frame body Z=1.6-3.2
# The frame is the outer rectangle minus the oval cutout
outer_rect = Rectangle(F_W, F_H)
inner_oval = Rectangle(OVAL_W, OVAL_H)

# Round the corners of outer rect and oval
outer_r = fillet(outer_rect.vertices(), 1.5)
inner_r = fillet(inner_oval.vertices(), 0.5)
inner_ellipse = round_rect = Rectangle(OVAL_W, OVAL_H)

# Better: use an actual ellipse/oval
inner_shape = Plane.XY * Rectangle(OVAL_W, OVAL_H)
inner_shape = fillet(inner_shape.vertices(), OVAL_H/4)  # Round to approximate oval

frame_shape = outer_rect - inner_shape
frame = extrude(frame_shape, Z2 - Z1)
frame = Pos(F_X, 0, Z1) * frame

# 3. Grooves on frame edges
# Create grooves along top and bottom edges of the frame
grooves = []
for i in range(N_GROOVES):
    gx = -F_W/2 + GROOVE_GAP/2 + i * (GROOVE_W + GROOVE_GAP)
    # Top groove
    g_top = Pos(F_X + gx, F_H/2, Z1) * Box(GROOVE_W, GROOVE_D+0.1, Z2-Z1)
    grooves.append(g_top)
    # Bottom groove
    g_bot = Pos(F_X + gx, -F_H/2, Z1) * Box(GROOVE_W, GROOVE_D+0.1, Z2-Z1)
    grooves.append(g_bot)

frame_with_grooves = frame
for g in grooves:
    frame_with_grooves -= g

# 4. Left collar Z=3.2-4.8
collar_shape = Rectangle(C_W, C_H)
collar_r = fillet(collar_shape.vertices(), 2.0)
collar = extrude(collar_r, Z3 - Z2)
collar = Pos(C_X, 0, Z2) * collar

# 5. Combine everything
body = base + frame_with_grooves + collar

# Add side indentations (2 per side, as seen in images)
indent_w = 1.0
indent_d = 0.5
indent_h = H * 0.6
for side in [-1, 1]:
    for i in [-1, 1]:
        indent = Pos(side * W/2, i * indent_h/3, Z1 + (Z2-Z1)/2) * Box(indent_w, indent_h/2, Z2-Z1)
        body -= indent

# Fillet sharp edges for smooth transitions
try:
    # Top corners of base
    e1 = body.edges().filter_by_position(Axis.Z, Z1-0.1, Z1+0.1)
    body = fillet(e1, 0.2)
except:
    pass

try:
    e2 = body.edges().filter_by_position(Axis.Z, Z2-0.1, Z2+0.1)
    body = fillet(e2, 0.2)
except:
    pass

# Export
stp_path = os.path.join(OUT, "window_connection.stp")
stl_path = os.path.join(OUT, "window_connection.stl")
export_step(body, stp_path)
export_stl(body, stl_path)

sz = body.bounding_box().size
print("STEP: window_connection.stp")
print("Dim: %.1f x %.1f x %.1f" % (sz.X, sz.Y, sz.Z))
print("WARNING: This is a BEST EFFORT reconstruction.")
print("Please verify against the original in Inventor.")
