"""Shell script generators for kind: backup_job steps.

Generates backup scripts for postgres_dump and directory backup types.
"""

from __future__ import annotations


def render_backup_script(
    *,
    name: str,
    source_type: str,
    destination_path: str,
    retention_count: int,
    # postgres_dump fields
    database: str | None = None,
    pg_host: str = "localhost",
    pg_port: int = 5432,
    pg_user: str = "postgres",
    docker_exec: str | None = None,
    # directory fields
    source_path: str | None = None,
) -> str:
    """Render a backup shell script."""
    lines: list[str] = [
        "#!/bin/bash",
        "set -euo pipefail",
        "",
        f"# Backup script for: {name}",
        "# Generated by nodeforge -- do not edit manually",
        "",
        f'BACKUP_DIR="{destination_path}"',
        'TIMESTAMP="$(date +%Y%m%d-%H%M%S)"',
        "",
        '# Ensure backup directory exists',
        'mkdir -p "$BACKUP_DIR"',
        "",
    ]

    if source_type == "postgres_dump":
        backup_file = f'"$BACKUP_DIR/{name}-$TIMESTAMP.sql.gz"'
        if docker_exec:
            lines.append('# Dump PostgreSQL database via docker exec')
            lines.append(
                f'docker exec {docker_exec} pg_dump -U {pg_user} {database} '
                f'| gzip > {backup_file}'
            )
        else:
            lines.append('# Dump PostgreSQL database')
            lines.append(
                f'pg_dump -h {pg_host} -p {pg_port} -U {pg_user} {database} '
                f'| gzip > {backup_file}'
            )
    elif source_type == "directory":
        backup_file = f'"$BACKUP_DIR/{name}-$TIMESTAMP.tar.gz"'
        lines.append('# Archive directory')
        lines.append(f'tar czf {backup_file} -C "$(dirname {source_path})" "$(basename {source_path})"')

    lines.extend([
        "",
        f"# Retention: keep {retention_count} most recent backups",
        'cd "$BACKUP_DIR"',
        f'ls -1t {name}-*.* 2>/dev/null | tail -n +{retention_count + 1} | xargs -r rm -f',
        "",
        f'echo "Backup completed: {name} at $TIMESTAMP"',
    ])

    return "\n".join(lines) + "\n"
