from __future__ import annotations

import argparse
from pathlib import Path


def build_real_bracket_model():
    """Build a deterministic simple bracket-like model using CadQuery."""
    import cadquery as cq

    # Deterministic geometry: base plate + 4 through holes + one vertical raised web.
    return (
        cq.Workplane("XY")
        .box(120.0, 80.0, 10.0, centered=(True, True, False))
        .faces(">Z")
        .workplane()
        .pushPoints([(-45.0, -25.0), (45.0, -25.0), (-45.0, 25.0), (45.0, 25.0)])
        .hole(10.0)
        .faces(">Z")
        .workplane()
        .center(0.0, 0.0)
        .rect(18.0, 50.0)
        .extrude(35.0)
    )


def main(argv: list[str] | None = None) -> int:
    parser = argparse.ArgumentParser(
        description=(
            "Generate examples/real_bracket.step using optional CadQuery/OCP.\n\n"
            "This script is optional and not required for the default test suite."
        ),
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )
    parser.add_argument(
        "--out",
        default="examples/real_bracket.step",
        help="Output STEP path (default: examples/real_bracket.step)",
    )
    parser.add_argument(
        "--overwrite",
        action="store_true",
        help="Overwrite existing output STEP file",
    )
    args = parser.parse_args(argv)

    out_path = Path(args.out)
    out_path.parent.mkdir(parents=True, exist_ok=True)

    if out_path.exists() and not args.overwrite:
        print(f"real STEP generator: output already exists: {out_path}")
        print("Use --overwrite to replace it.")
        return 1

    try:
        import cadquery as cq
    except Exception as exc:
        print(
            "real STEP generator: optional dependency missing (CadQuery/OCP).\n"
            "Install optional dependency and rerun:\n"
            "  pip install cadquery\n"
            f"Details: {exc}"
        )
        return 2

    try:
        model = build_real_bracket_model()
        cq.exporters.export(model, str(out_path))
    except Exception as exc:
        print(f"real STEP generator: failed to export STEP: {exc}")
        return 1

    size = out_path.stat().st_size if out_path.exists() else 0
    if size <= 0:
        print("real STEP generator: output STEP is empty")
        return 1

    print(f"real STEP generator: wrote {out_path} ({size} bytes)")
    print("Model: base plate + 4 through holes + raised web")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
