"""phase ds5 secure local download streaming requests

Revision ID: 20260528_phase_ds5_secure_download_streaming
Revises: 20260527_phase_ds3_ds4_storage_jobs
Create Date: 2026-05-14
"""
from __future__ import annotations

from alembic import op
import sqlalchemy as sa


revision = "20260528_phase_ds5_secure_download_streaming"
down_revision = "20260527_phase_ds3_ds4_storage_jobs"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "document_download_requests",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("tenant_id", sa.Integer(), nullable=False),
        sa.Column("branch_id", sa.Integer(), nullable=True),
        sa.Column("storage_node_id", sa.Integer(), nullable=False),
        sa.Column("document_id", sa.Integer(), nullable=False),
        sa.Column("version_id", sa.Integer(), nullable=False),
        sa.Column("request_status", sa.String(length=30), nullable=False, server_default="pending"),
        sa.Column("local_relative_path", sa.String(length=1000), nullable=True),
        sa.Column("expected_hash_sha256", sa.String(length=64), nullable=False),
        sa.Column("cached_relative_path", sa.String(length=1000), nullable=True),
        sa.Column("cached_hash_sha256", sa.String(length=64), nullable=True),
        sa.Column("file_size_bytes", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("attempts", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("last_error", sa.Text(), nullable=True),
        sa.Column("requested_by_user_id", sa.Integer(), nullable=True),
        sa.Column("requested_ip", sa.String(length=80), nullable=True),
        sa.Column("requested_user_agent", sa.String(length=500), nullable=True),
        sa.Column("created_at_utc", sa.DateTime(timezone=True), nullable=False),
        sa.Column("fulfilled_at_utc", sa.DateTime(timezone=True), nullable=True),
        sa.Column("failed_at_utc", sa.DateTime(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(["tenant_id"], ["tenants.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["branch_id"], ["branches.id"], ondelete="SET NULL"),
        sa.ForeignKeyConstraint(["storage_node_id"], ["branch_storage_nodes.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["document_id"], ["engagement_documents.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["version_id"], ["engagement_document_versions.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["requested_by_user_id"], ["users.id"], ondelete="SET NULL"),
        sa.PrimaryKeyConstraint("id"),
    )
    for col in [
        "tenant_id",
        "branch_id",
        "storage_node_id",
        "document_id",
        "version_id",
        "request_status",
        "expected_hash_sha256",
        "requested_by_user_id",
    ]:
        op.create_index(f"ix_document_download_requests_{col}", "document_download_requests", [col])


def downgrade() -> None:
    op.drop_table("document_download_requests")
