"""Rename software nodes and add the professional software job ledger.

Revision ID: 0032
Revises: 0031
Create Date: 2026-08-13
"""
from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

revision: str = "0032"
down_revision: str | None = "0031"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    op.rename_table("compute_node_enrollments", "software_node_enrollments")
    op.rename_table("compute_nodes", "software_nodes")
    op.execute(
        "ALTER INDEX ix_compute_nodes_status RENAME TO ix_software_nodes_status"
    )
    op.execute(
        "ALTER TABLE software_node_enrollments RENAME CONSTRAINT "
        "compute_node_enrollments_pkey TO software_node_enrollments_pkey"
    )
    op.execute(
        "ALTER TABLE software_node_enrollments RENAME CONSTRAINT "
        "compute_node_enrollments_code_hash_key TO software_node_enrollments_code_hash_key"
    )
    op.execute(
        "ALTER TABLE software_node_enrollments RENAME CONSTRAINT "
        "compute_node_enrollments_created_by_fkey TO software_node_enrollments_created_by_fkey"
    )
    op.execute(
        "ALTER TABLE software_nodes RENAME CONSTRAINT "
        "compute_nodes_pkey TO software_nodes_pkey"
    )
    op.execute(
        "ALTER TABLE software_nodes RENAME CONSTRAINT "
        "compute_nodes_install_id_key TO software_nodes_install_id_key"
    )
    op.create_table(
        "software_jobs",
        sa.Column("job_id", postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column("user_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("users.user_id", ondelete="CASCADE"), nullable=False),
        sa.Column("task_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("tasks.task_id", ondelete="CASCADE"), nullable=False),
        sa.Column("idempotency_key", sa.Text(), nullable=False),
        sa.Column("capability", sa.Text(), nullable=False),
        sa.Column("request", postgresql.JSONB(), nullable=False),
        sa.Column("request_digest", sa.Text(), nullable=False),
        sa.Column("input_manifest", postgresql.JSONB(), nullable=False),
        sa.Column("node_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("software_nodes.node_id", ondelete="SET NULL"), nullable=True),
        sa.Column("lease_id", postgresql.UUID(as_uuid=True), nullable=True),
        sa.Column("lease_expires_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("status", sa.Text(), server_default="queued", nullable=False),
        sa.Column("stage", sa.Text(), server_default="", nullable=False),
        sa.Column("progress", sa.Integer(), server_default="0", nullable=False),
        sa.Column("metrics", postgresql.JSONB(), nullable=False),
        sa.Column("error", postgresql.JSONB(), nullable=False),
        sa.Column("artifact_manifest", postgresql.JSONB(), nullable=False),
        sa.Column("started_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("terminal_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.UniqueConstraint("user_id", "idempotency_key", name="uq_software_jobs_user_idempotency"),
    )
    op.create_index("ix_software_jobs_status_created", "software_jobs", ["status", "created_at"])
    op.create_index("ix_software_jobs_node_status", "software_jobs", ["node_id", "status"])


def downgrade() -> None:
    op.drop_index("ix_software_jobs_node_status", table_name="software_jobs")
    op.drop_index("ix_software_jobs_status_created", table_name="software_jobs")
    op.drop_table("software_jobs")
    op.execute(
        "ALTER TABLE software_nodes RENAME CONSTRAINT "
        "software_nodes_install_id_key TO compute_nodes_install_id_key"
    )
    op.execute(
        "ALTER TABLE software_nodes RENAME CONSTRAINT "
        "software_nodes_pkey TO compute_nodes_pkey"
    )
    op.execute(
        "ALTER TABLE software_node_enrollments RENAME CONSTRAINT "
        "software_node_enrollments_created_by_fkey TO compute_node_enrollments_created_by_fkey"
    )
    op.execute(
        "ALTER TABLE software_node_enrollments RENAME CONSTRAINT "
        "software_node_enrollments_code_hash_key TO compute_node_enrollments_code_hash_key"
    )
    op.execute(
        "ALTER TABLE software_node_enrollments RENAME CONSTRAINT "
        "software_node_enrollments_pkey TO compute_node_enrollments_pkey"
    )
    op.execute(
        "ALTER INDEX ix_software_nodes_status RENAME TO ix_compute_nodes_status"
    )
    op.rename_table("software_nodes", "compute_nodes")
    op.rename_table("software_node_enrollments", "compute_node_enrollments")
