"""adding asset_id and asset_type to job table for polymorphism of jobs.

Revision ID: 26ef53e27502
Revises: 169b35510b37
Create Date: 2026-02-12 16:42:09.706216

Phase: EXPAND
"""

from collections.abc import Sequence

import sqlalchemy as sa
import sqlmodel
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "26ef53e27502"
down_revision: str | None = "169b35510b37"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    conn = op.get_bind()
    # Check which columns/indexes already exist (handles fresh DB where model creates them)
    inspector = sa.inspect(conn)
    existing_columns = {col["name"] for col in inspector.get_columns("job")}
    existing_indexes = {idx["name"] for idx in inspector.get_indexes("job")}

    with op.batch_alter_table("job", schema=None) as batch_op:
        if "asset_id" not in existing_columns:
            batch_op.add_column(sa.Column("asset_id", sa.Uuid(), nullable=True))
        if "asset_type" not in existing_columns:
            batch_op.add_column(sa.Column("asset_type", sqlmodel.sql.sqltypes.AutoString(), nullable=True))
        if "ix_job_asset_id" not in existing_indexes:
            batch_op.create_index(batch_op.f("ix_job_asset_id"), ["asset_id"], unique=False)

    # ### end Alembic commands ###


def downgrade() -> None:
    conn = op.get_bind()
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table("job", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_job_asset_id"))
        batch_op.drop_column("asset_type")
        batch_op.drop_column("asset_id")

    # ### end Alembic commands ###
