"""Blender headless script: export a STEP-derived scene as a production .blend.

Usage:
    blender --background --python export_blend.py -- \\
        --stl_path /path/to/file.stl \\
        --output_path /path/to/output.blend \\
        [--asset_library_blend /path/to/library.blend] \\
        [--material_map '{"SrcMat": "LibMat"}']

The script:
1. Imports the STL file (with mm→m scale).
2. Optionally applies asset library materials from a .blend.
3. Packs all external data.
4. Saves a copy as the output .blend.
"""
from __future__ import annotations

import argparse
import json
import sys
import traceback


def parse_args() -> argparse.Namespace:
    argv = sys.argv
    if "--" not in argv:
        print("No arguments after --", file=sys.stderr)
        sys.exit(1)
    rest = argv[argv.index("--") + 1:]
    parser = argparse.ArgumentParser()
    parser.add_argument("--stl_path", required=True)
    parser.add_argument("--output_path", required=True)
    parser.add_argument("--asset_library_blend", default=None)
    parser.add_argument("--material_map", default="{}")
    return parser.parse_args(rest)


def main() -> None:
    args = parse_args()
    material_map: dict = json.loads(args.material_map)

    import bpy  # type: ignore[import]

    # Clean scene
    bpy.ops.wm.read_factory_settings(use_empty=True)

    # Import STL
    bpy.ops.import_mesh.stl(filepath=args.stl_path)

    # Scale mm → m
    for obj in bpy.context.selected_objects:
        obj.scale = (0.001, 0.001, 0.001)
        bpy.context.view_layer.objects.active = obj
        bpy.ops.object.transform_apply(scale=True)

    # Apply asset library materials if provided
    if args.asset_library_blend and material_map:
        import os
        sys.path.insert(0, os.path.dirname(__file__))
        from asset_library import apply_asset_library_materials
        apply_asset_library_materials(args.asset_library_blend, material_map)

    # Pack all external data into the .blend
    bpy.ops.file.pack_all()

    # Save a copy to output_path
    bpy.ops.wm.save_as_mainfile(filepath=args.output_path, compress=True, copy=True)

    print(f".blend exported to {args.output_path}")


try:
    main()
except SystemExit:
    raise
except Exception:
    traceback.print_exc()
    sys.exit(1)
