
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 = 'Adjustable carton magazine side guide plate'
MATERIAL = 'Stainless steel'
REVISION = 'rev_a'
FALLBACK_MODE = False

def build_model():

    base_plate = box(450.0, 220.0, 3.0, center=(0.0, 0.0, 1.5))
    left_flange = box(450.0, 3.0, 25.0, center=(0.0, -108.5, 14.0))
    right_flange = box(450.0, 3.0, 25.0, center=(0.0, 108.5, 14.0))
    lead_in = box(40.0, 220.0, 2.0, center=(205.0, 0.0, 2.5))
    base = fuse_all([base_plate, left_flange, right_flange, lead_in])
    for slot_x in (-120.0, 0.0, 120.0):
        base = cut_box(base, 60.0, 12.0, 6.0, center=(slot_x, -20.0, 1.5))
    pass
    pass
    if not FALLBACK_MODE:
        base = cut_hole(base, 8.0, 8.0, center=(-200.0, 0.0, 1.5), axis="z")
    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()
