#!/usr/bin/env python3
"""
export_quick_fix_engineering_to_csv.py

Exports Installed Hotfixes (Win32_QuickFixEngineering) to a timestamped CSV file using the
built-in `wmic` command, stdlib only.

Usage:
    python export_quick_fix_engineering_to_csv.py [output_folder]
"""

import subprocess
import sys
from datetime import datetime
from pathlib import Path


def main() -> None:
    out_folder = Path(sys.argv[1]) if len(sys.argv) > 1 else Path(".")
    out_folder.mkdir(parents=True, exist_ok=True)

    timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
    out_path = out_folder / f"quick_fix_engineering_{timestamp}.csv"

    result = subprocess.run(
        ["wmic", "path", "Win32_QuickFixEngineering", "get", "HotFixID,Description,InstalledOn,InstalledBy", "/format:csv"],
        capture_output=True, text=True, check=False,
    )

    if result.returncode != 0:
        sys.exit(f"wmic failed: {result.stderr.strip()}")

    out_path.write_text(result.stdout, encoding="utf-8")
    print(f"Exported Installed Hotfixes to {out_path}")


if __name__ == "__main__":
    main()