"""Package Tableau templates into .twbx archives."""

from __future__ import annotations

import zipfile
from pathlib import Path

from g_gremlin.tableau.xml_utils import get_extract_relpath_from_template


def package_workbook(twb_path: Path, hyper_path: Path, output_twbx_path: Path) -> Path:
    """
    Create .twbx (Tableau Packaged Workbook) by zipping TWB + Hyper.

    The Hyper file is placed at the exact relative path referenced in the template.
    """
    output_twbx_path.parent.mkdir(parents=True, exist_ok=True)
    extract_relpath = get_extract_relpath_from_template(twb_path)

    with zipfile.ZipFile(output_twbx_path, "w", zipfile.ZIP_DEFLATED) as zf:
        zf.write(twb_path, arcname=twb_path.name)
        zf.write(hyper_path, arcname=extract_relpath)

    return output_twbx_path


__all__ = ["package_workbook"]
