"""Backup utilities for gallery."""

import datetime
import zipfile
from pathlib import Path


def create_backup(web_folder: Path, backup_folder: Path) -> bool:
    """
    Create a backup of the web folder.

    Args:
        web_folder: Path to the web folder to backup
        backup_folder: Path to the backup directory

    Returns:
        True if backup was created successfully, False otherwise
    """
    try:
        today = datetime.date.today().strftime("%Y%m%d")
        backup_name = f"backup-{today}.zip"
        backup_path = backup_folder / backup_name

        backup_folder.mkdir(parents=True, exist_ok=True)

        if backup_path.exists():
            return True

        with zipfile.ZipFile(backup_path, "w", zipfile.ZIP_DEFLATED) as zipf:
            for path in web_folder.rglob("*"):
                if path.is_file():
                    arcname = path.relative_to(web_folder.parent)
                    zipf.write(path, arcname)
        return True
    except Exception as e:
        print(f"Warning: Could not create backup: {e}")
        return False
