#!/usr/bin/env python3
"""Archive PrestaShop amexportcompta prête pour Addons et livraison WooCommerce."""

from __future__ import annotations

import zipfile
from pathlib import Path

MODULE_SRC = Path("/Users/arnaudmerigeau/Locals/ps17/modules/amexportcompta")
OUT_DIR = Path(__file__).resolve().parents[1] / "exports" / "amexportcompta-product"
MODULE_NAME = "amexportcompta"
VERSION = "3.9.6"

EXCLUDE_DIR_NAMES = {".git", "__MACOSX", ".cursor"}
EXCLUDE_FILE_NAMES = {
    ".DS_Store",
    ".gitignore",
    "config_fr.xml",
    ".php-cs-fixer.dist.php",
    ".php-cs-fixer.cache",
}
EXCLUDE_SUFFIXES = {".csv"}


def should_include(path: Path) -> bool:
    rel = path.relative_to(MODULE_SRC)
    for part in rel.parts:
        if part in EXCLUDE_DIR_NAMES:
            return False
    if path.name in EXCLUDE_FILE_NAMES:
        return False
    if path.suffix.lower() in EXCLUDE_SUFFIXES:
        return False
    return True


def create_zip() -> Path:
    OUT_DIR.mkdir(parents=True, exist_ok=True)
    zip_path = OUT_DIR / f"{MODULE_NAME}-{VERSION}.zip"
    with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
        for path in sorted(MODULE_SRC.rglob("*")):
            if not path.is_file() or not should_include(path):
                continue
            arcname = f"{MODULE_NAME}/{path.relative_to(MODULE_SRC).as_posix()}"
            zf.write(path, arcname)
    return zip_path


def main() -> None:
    zip_path = create_zip()
    size_kb = zip_path.stat().st_size // 1024
    print(f"Archive : {zip_path}")
    print(f"Taille  : {size_kb} Ko")


if __name__ == "__main__":
    main()
