"""Add app_configs table for frontend configuration

Revision ID: d811b103dbfc
Revises: 09206ac04fd3
Create Date: 2025-10-26 15:11:41.781361

"""

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

from ai.backend.manager.models.base import GUID

# revision identifiers, used by Alembic.
revision = "d811b103dbfc"
down_revision = "09206ac04fd3"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "app_configs",
        sa.Column("id", GUID(), server_default=sa.text("uuid_generate_v4()"), nullable=False),
        sa.Column(
            "scope_type",
            sa.Enum("DOMAIN", "PROJECT", "USER", name="app_config_scope_type"),
            nullable=False,
        ),
        sa.Column("scope_id", sa.String(length=256), nullable=False),
        sa.Column("extra_config", postgresql.JSONB(astext_type=sa.Text()), nullable=False),
        sa.Column(
            "created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=True
        ),
        sa.Column(
            "modified_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("now()"),
            nullable=True,
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_app_configs")),
        sa.UniqueConstraint("scope_type", "scope_id", name="uq_app_configs_scope"),
    )
    op.create_index(op.f("ix_app_configs_scope_id"), "app_configs", ["scope_id"], unique=False)
    op.create_index(op.f("ix_app_configs_scope_type"), "app_configs", ["scope_type"], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f("ix_app_configs_scope_type"), table_name="app_configs")
    op.drop_index(op.f("ix_app_configs_scope_id"), table_name="app_configs")
    op.drop_table("app_configs")
    op.execute("DROP TYPE IF EXISTS app_config_scope_type")
    # ### end Alembic commands ###
