"""add oem_id and agent_id to card_keys

Revision ID: 007
Revises: 006
Create Date: 2026-05-21

"""

from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

revision: str = "007"
down_revision: Union[str, None] = "006"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.add_column(
        "card_keys",
        sa.Column("oem_id", sa.Integer(), nullable=True),
    )
    op.add_column(
        "card_keys",
        sa.Column("agent_id", sa.Integer(), nullable=True),
    )
    op.create_foreign_key(
        "fk_card_keys_oem_id_users",
        "card_keys",
        "users",
        ["oem_id"],
        ["id"],
        ondelete="SET NULL",
    )
    op.create_foreign_key(
        "fk_card_keys_agent_id_users",
        "card_keys",
        "users",
        ["agent_id"],
        ["id"],
        ondelete="SET NULL",
    )
    op.create_index(op.f("ix_card_keys_oem_id"), "card_keys", ["oem_id"], unique=False)
    op.create_index(op.f("ix_card_keys_agent_id"), "card_keys", ["agent_id"], unique=False)


def downgrade() -> None:
    op.drop_index(op.f("ix_card_keys_agent_id"), table_name="card_keys")
    op.drop_index(op.f("ix_card_keys_oem_id"), table_name="card_keys")
    op.drop_constraint("fk_card_keys_agent_id_users", "card_keys", type_="foreignkey")
    op.drop_constraint("fk_card_keys_oem_id_users", "card_keys", type_="foreignkey")
    op.drop_column("card_keys", "agent_id")
    op.drop_column("card_keys", "oem_id")
