"""gpu_job: the HTTP-leased GPU work queue for the desktop agent (#114)

The agent stays HTTP-only — the server enqueues per-(image, task) jobs here and
the agent leases/submits over the web API; Redis/Postgres stay private.

Revision ID: 0062
Revises: 0061
Create Date: 2026-06-29
"""
from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

revision: str = "0062"
down_revision: Union[str, None] = "0061"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        "gpu_job",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column(
            "image_record_id", sa.Integer(),
            sa.ForeignKey("image_record.id", ondelete="CASCADE"), nullable=False,
        ),
        sa.Column("task", sa.String(length=32), nullable=False),
        sa.Column(
            "status", sa.String(length=16), nullable=False,
            server_default="pending",
        ),
        sa.Column("lease_token", sa.String(length=64), nullable=True),
        sa.Column("leased_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("lease_expires_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("attempts", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("error", sa.Text(), nullable=True),
        sa.Column(
            "created_at", sa.DateTime(timezone=True), nullable=False,
            server_default=sa.func.now(),
        ),
        sa.Column(
            "updated_at", sa.DateTime(timezone=True), nullable=False,
            server_default=sa.func.now(),
        ),
    )
    op.create_index("ix_gpu_job_image_record_id", "gpu_job", ["image_record_id"])
    op.create_index("ix_gpu_job_status", "gpu_job", ["status"])


def downgrade() -> None:
    op.drop_index("ix_gpu_job_status", table_name="gpu_job")
    op.drop_index("ix_gpu_job_image_record_id", table_name="gpu_job")
    op.drop_table("gpu_job")
