"""phase ds3 ds4 branch storage nodes and document jobs

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

from alembic import op
import sqlalchemy as sa


revision = "20260527_phase_ds3_ds4_storage_jobs"
down_revision = "20260526_phase_ds1_ds2_documents"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "branch_storage_nodes",
        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("node_code", sa.String(length=80), nullable=False),
        sa.Column("node_name", sa.String(length=200), nullable=False),
        sa.Column("connector_url", sa.String(length=500), nullable=True),
        sa.Column("secret_key_hash", sa.String(length=64), nullable=False),
        sa.Column("storage_root_path", sa.String(length=1000), nullable=True),
        sa.Column("storage_mode", sa.String(length=40), nullable=False, server_default="pull_jobs"),
        sa.Column("status", sa.String(length=30), nullable=False, server_default="active"),
        sa.Column("is_active", sa.Boolean(), nullable=False, server_default=sa.true()),
        sa.Column("last_seen_at_utc", sa.DateTime(timezone=True), nullable=True),
        sa.Column("last_seen_ip", sa.String(length=80), nullable=True),
        sa.Column("quota_limit_bytes", sa.Integer(), nullable=True),
        sa.Column("used_storage_bytes", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("subscription_required", sa.Boolean(), nullable=False, server_default=sa.true()),
        sa.Column("created_by_user_id", sa.Integer(), nullable=True),
        sa.Column("created_at_utc", sa.DateTime(timezone=True), nullable=False),
        sa.Column("updated_at_utc", sa.DateTime(timezone=True), nullable=False),
        sa.ForeignKeyConstraint(["tenant_id"], ["tenants.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["branch_id"], ["branches.id"], ondelete="SET NULL"),
        sa.ForeignKeyConstraint(["created_by_user_id"], ["users.id"], ondelete="SET NULL"),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("tenant_id", "branch_id", "node_code", name="uq_branch_storage_node_code"),
    )
    for col in ["tenant_id", "branch_id", "node_code", "storage_mode", "status", "is_active"]:
        op.create_index(f"ix_branch_storage_nodes_{col}", "branch_storage_nodes", [col])

    op.create_table(
        "document_storage_jobs",
        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("job_type", sa.String(length=40), nullable=False, server_default="store_version"),
        sa.Column("status", sa.String(length=30), nullable=False, server_default="pending"),
        sa.Column("priority", sa.Integer(), nullable=False, server_default="5"),
        sa.Column("staging_relative_path", sa.String(length=1000), nullable=False),
        sa.Column("target_relative_path", sa.String(length=1000), nullable=False),
        sa.Column("file_size_bytes", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("expected_hash_sha256", sa.String(length=64), nullable=False),
        sa.Column("attempts", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("last_error", sa.Text(), nullable=True),
        sa.Column("created_at_utc", sa.DateTime(timezone=True), nullable=False),
        sa.Column("picked_at_utc", sa.DateTime(timezone=True), nullable=True),
        sa.Column("completed_at_utc", sa.DateTime(timezone=True), nullable=True),
        sa.Column("acknowledged_hash_sha256", sa.String(length=64), nullable=True),
        sa.Column("local_final_path", sa.String(length=1000), nullable=True),
        sa.Column("created_by_user_id", sa.Integer(), 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(["created_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", "job_type", "status", "priority", "expected_hash_sha256"]:
        op.create_index(f"ix_document_storage_jobs_{col}", "document_storage_jobs", [col])


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