
from __future__ import annotations

import sys
from pathlib import Path

import FreeCAD
import Part
from FreeCAD import Vector


def box(length, width, height, center):
    cx, cy, cz = center
    return Part.makeBox(
        float(length),
        float(width),
        float(height),
        Vector(cx - (length / 2.0), cy - (width / 2.0), cz - (height / 2.0)),
    )


def cylinder(diameter, length, center, axis="z"):
    cx, cy, cz = center
    radius = float(diameter) / 2.0
    if axis == "x":
        base = Vector(cx - (length / 2.0), cy, cz)
        direction = Vector(1, 0, 0)
    elif axis == "y":
        base = Vector(cx, cy - (length / 2.0), cz)
        direction = Vector(0, 1, 0)
    else:
        base = Vector(cx, cy, cz - (length / 2.0))
        direction = Vector(0, 0, 1)
    return Part.makeCylinder(radius, float(length), base, direction)


def fuse_all(shapes):
    items = [shape for shape in shapes if shape is not None]
    if not items:
        raise ValueError("No shapes were provided for fusion.")
    result = items[0]
    for other in items[1:]:
        result = result.fuse(other)
    return result


def make_compound(shapes):
    items = [shape for shape in shapes if shape is not None]
    if len(items) == 1:
        return items[0]
    return Part.makeCompound(items)


def cut_box(shape, length, width, height, center):
    return shape.cut(box(length, width, height, center))


def cut_hole(shape, diameter, length, center, axis="z"):
    return shape.cut(cylinder(diameter, length, center, axis=axis))


def add_box(shape, length, width, height, center):
    return shape.fuse(box(length, width, height, center))


def add_cylinder(shape, diameter, length, center, axis="z"):
    return shape.fuse(cylinder(diameter, length, center, axis=axis))


def export_shape(shape, output_path):
    output = Path(output_path).resolve()
    output.parent.mkdir(parents=True, exist_ok=True)
    refined = shape.removeSplitter() if hasattr(shape, "removeSplitter") else shape
    refined.exportStep(str(output))
    print(str(output))


DISPLAY_NAME = 'Hot-melt nozzle mounting bracket with heat shield'
MATERIAL = 'Stainless steel'
REVISION = 'rev_a'
FALLBACK_MODE = False

def build_model():

    carrier = box(180.0, 2.0, 120.0, center=(0.0, 0.0, 60.0))
    support_leg = box(32.0, 36.0, 2.0, center=(0.0, 18.0, 18.0))
    heat_shield = box(220.0, 1.6, 160.0, center=(0.0, 34.0, 80.0))
    standoff_left = box(10.0, 32.0, 10.0, center=(-70.0, 18.0, 60.0))
    standoff_right = box(10.0, 32.0, 10.0, center=(70.0, 18.0, 60.0))
    carrier = cut_box(carrier, 18.0, 2.8, 12.0, center=(-24.0, 0.0, 42.0))
    carrier = cut_box(carrier, 18.0, 2.8, 12.0, center=(-24.0, 0.0, 78.0))
    pass
    heat_shield = cut_hole(heat_shield, 8.0, 8.0, center=(-60.0, 34.0, 40.0), axis="y")
    heat_shield = cut_hole(heat_shield, 8.0, 8.0, center=(-20.0, 34.0, 40.0), axis="y")
    heat_shield = cut_hole(heat_shield, 8.0, 8.0, center=(20.0, 34.0, 40.0), axis="y")
    heat_shield = cut_hole(heat_shield, 8.0, 8.0, center=(60.0, 34.0, 40.0), axis="y")
    shape = make_compound([carrier, support_leg, heat_shield, standoff_left, standoff_right])
    return shape

def main():
    if len(sys.argv) != 2:
        raise SystemExit("Usage: freecad_script.py <output.step>")
    shape = build_model()
    export_shape(shape, sys.argv[1])

if __name__ == "__main__":
    main()
