
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 = 'Lug-chain carton pocket side plate'
MATERIAL = 'Al 7075-T6'
REVISION = 'rev_b'
FALLBACK_MODE = False

def build_model():

    base = box(260.0, 140.0, 12.0, center=(0.0, 0.0, 6.0))
    base = cut_box(base, 116.0, 75.5, 7.0, center=(18.0, 0.0, 8.5))
    for wear_y in (-42.0, -14.0, 14.0, 42.0):
        base = cut_hole(base, 6.0, 16.0, center=(-82.0, wear_y, 6.0), axis="z")
    base = cut_hole(base, 8.0, 16.0, center=(78.3, 32.0, 6.0), axis="z")
    base = cut_hole(base, 8.0, 16.0, center=(78.0, -32.0, 6.0), axis="z")
    base = cut_hole(base, 10.0, 16.0, center=(-106.0, -46.0, 6.0), axis="z")
    base = cut_hole(base, 10.0, 16.0, center=(-106.0, 46.0, 6.0), axis="z")
    base = cut_box(base, 26.0, 20.0, 4.0, center=(92.0, 0.0, 10.0))
    if not FALLBACK_MODE:
        base = cut_box(base, 18.0, 96.0, 3.0, center=(72.0, 0.0, 10.5))
    shape = base
    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()
