"""Backup-pending marker inserts — SP17 encrypted DB backups.

``add_backup_pending()`` inserts a ``pending`` row for a backup that is
about to start; the BackupService updates ``status`` / ``size_bytes``
/ ``db_fingerprint`` / ``table_count`` / ``completed_at`` after the
encrypted blob lands on disk.
"""

from cyclone import db

from . import utcnow


# -- SP17: encrypted DB backups -------------------------------------

def add_backup_pending(*, filename: str, backup_dir: str) -> db.DbBackup:
    """Insert a ``pending`` row for a backup that is about to start.

    The BackupService fills in ``status`` / ``size_bytes`` /
    ``db_fingerprint`` / ``table_count`` / ``completed_at`` after
    the encrypted blob lands on disk.
    """
    with db.SessionLocal()() as s:
        row = db.DbBackup(
            filename=filename,
            backup_dir=backup_dir,
            size_bytes=0,
            db_fingerprint=None,
            table_count=0,
            created_at=utcnow(),
            completed_at=None,
            status="pending",
            error_message=None,
        )
        s.add(row)
        s.commit()
        s.refresh(row)
        return row

