#!/usr/bin/env python3
"""CLI entrypoint to export GeoData assets for Unity."""
from __future__ import annotations

import argparse
import os
import sys
from typing import Iterable

from geodata_pipeline.config import Config, DEFAULT_CONFIG_PATH, ensure_default_config
from geodata_pipeline.buildings import export_buildings
from geodata_pipeline.buildings_enhanced import export_buildings_enhanced
from geodata_pipeline.heightmaps import export_heightmaps
from geodata_pipeline.heightmaps_enhanced import export_heightmaps_enhanced
from geodata_pipeline.orthophotos import export_orthophotos
from geodata_pipeline.setup_helpers import ensure_directories, materialize_archives
from geodata_pipeline.street_furniture import export_street_furniture
from geodata_pipeline.trees import export_trees
from geodata_pipeline.trees_enhanced import export_trees_enhanced


def parse_args(argv: Iterable[str] | None = None) -> argparse.Namespace:
    parser = argparse.ArgumentParser(description="Export GeoData assets for Unity.")
    parser.add_argument(
        "--config",
        default=DEFAULT_CONFIG_PATH,
        help="Path to config JSON (created on --setup if missing).",
    )
    parser.add_argument(
        "--export",
        choices=[
            "heightmap", "textures", "buildings", "trees", "all",
            "heightmap-enhanced", "buildings-enhanced", "trees-enhanced",
            "street-furniture", "all-enhanced"
        ],
        default=None,
        help="Which assets to export. Enhanced options use point cloud data.",
    )
    parser.add_argument("--raw-dgm1-path", dest="raw_dgm1_path", help="Override raw DGM1 directory.")
    parser.add_argument("--raw-dop20-path", dest="raw_dop20_path", help="Override raw DOP20 JP2 directory.")
    parser.add_argument(
        "--build-from-archive",
        action="store_true",
        help="Populate raw inputs from archives (unzips zips, leaves dop20 filelist in archive).",
    )
    parser.add_argument(
        "--setup",
        action="store_true",
        help="Create default directory structure and config if missing; does not export.",
    )
    parser.add_argument(
        "--force-vrt",
        action="store_true",
        help="Rebuild VRTs even if present (useful after moving raw data).",
    )
    return parser.parse_args(argv)


def load_config(args: argparse.Namespace) -> Config:
    if args.setup:
        cfg = Config.default()
        cfg.save(args.config)
        return cfg.with_overrides(raw_dgm1_path=args.raw_dgm1_path, raw_dop20_path=args.raw_dop20_path)
    if os.path.exists(args.config):
        cfg = Config.load(args.config)
    else:
        cfg = Config.default()
        cfg.save(args.config)
    return cfg.with_overrides(raw_dgm1_path=args.raw_dgm1_path, raw_dop20_path=args.raw_dop20_path)


def main(argv: Iterable[str] | None = None) -> int:
    args = parse_args(argv)
    cfg = load_config(args)
    target_export = args.export or "all"

    if args.setup:
        ensure_directories(cfg)
        print(f"Directories ensured. Config at {args.config}.")
        if args.build_from_archive:
            materialize_archives(cfg)
        if args.export is None:
            return 0

    if args.build_from_archive and not args.setup:
        materialize_archives(cfg)

    exit_codes = []

    # Standard exports
    if target_export in ("heightmap", "all"):
        exit_codes.append(export_heightmaps(cfg, force_vrt=args.force_vrt))
    if target_export in ("textures", "all"):
        exit_codes.append(export_orthophotos(cfg, force_vrt=args.force_vrt))
    if target_export in ("buildings", "all"):
        exit_codes.append(export_buildings(cfg))
    if target_export in ("trees", "all"):
        exit_codes.append(export_trees(cfg))

    # Enhanced exports (use point cloud data)
    # Order matters: heightmap-enhanced creates tile_index.csv needed by others
    # street-furniture must run before trees-enhanced (for exclusion mask)
    if target_export in ("heightmap-enhanced", "all-enhanced"):
        exit_codes.append(export_heightmaps_enhanced(cfg))
    if target_export in ("all-enhanced",):
        exit_codes.append(export_orthophotos(cfg, force_vrt=args.force_vrt))
    if target_export in ("street-furniture", "all-enhanced"):
        exit_codes.append(export_street_furniture(cfg))
    if target_export in ("buildings-enhanced", "all-enhanced"):
        exit_codes.append(export_buildings_enhanced(cfg))
    if target_export in ("trees-enhanced", "all-enhanced"):
        exit_codes.append(export_trees_enhanced(cfg))

    return max(exit_codes) if exit_codes else 0


if __name__ == "__main__":
    sys.exit(main())
