"""FC-3h: backup_run — operator-facing artifact record for a backup run.

One row per backup attempt (kind='db' or 'images'). Lifecycle
tracking (started_at/finished_at/duration_ms/exception text) lives
in task_run from FC-3i — this row records artifact metadata: file
paths, sizes, tag (retention protection), and restore lineage via
restored_from_id.

Status values (String, not Postgres ENUM — per
feedback_check_existing_enums):
  pending   — created but task hasn't started yet (rare; usually
              status starts as 'running' from the task body).
  running   — backup task is in flight.
  ok        — artifact successfully written.
  error     — task raised; error column populated.
  restoring — this row represents a restore attempt (kind = restored
              kind); linked to source via restored_from_id.
  restored  — restore completed successfully.
"""

from datetime import datetime

from sqlalchemy import JSON, BigInteger, DateTime, ForeignKey, Integer, String, Text
from sqlalchemy.orm import Mapped, mapped_column

from .base import Base


class BackupRun(Base):
    __tablename__ = "backup_run"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    kind: Mapped[str] = mapped_column(String(16), nullable=False, index=True)
    status: Mapped[str] = mapped_column(
        String(16), nullable=False, default="pending", index=True,
    )
    tag: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)
    triggered_by: Mapped[str] = mapped_column(String(32), nullable=False)
    started_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), nullable=False, index=True,
    )
    finished_at: Mapped[datetime | None] = mapped_column(
        DateTime(timezone=True), nullable=True, index=True,
    )
    sql_path: Mapped[str | None] = mapped_column(Text, nullable=True)
    tar_path: Mapped[str | None] = mapped_column(Text, nullable=True)
    size_bytes: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    error: Mapped[str | None] = mapped_column(Text, nullable=True)
    manifest: Mapped[dict] = mapped_column(
        JSON, nullable=False, default=dict, server_default="{}",
    )
    restored_from_id: Mapped[int | None] = mapped_column(
        ForeignKey("backup_run.id", ondelete="SET NULL"),
        nullable=True,
    )
