"""add tool_configs table

Revision ID: 0009_tool_configs
Revises: 0008_tool_type_category
Create Date: 2026-05-20 09:00:00.000000

"""
from typing import Sequence, Union

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

# revision identifiers, used by Alembic.
revision: str = "0009_tool_configs"
down_revision: Union[str, None] = "0008_tool_type_category"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        "tool_configs",
        sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False),
        sa.Column("user_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("tool_type_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("project_id", postgresql.UUID(as_uuid=True), nullable=True),
        sa.Column("key", sa.String(255), nullable=False),
        sa.Column("value", sa.Text(), nullable=False),
        sa.Column("config_type", sa.String(20), nullable=False, server_default="env"),
        sa.Column("file_path", sa.String(1024), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.ForeignKeyConstraint(["user_id"], ["users.id"]),
        sa.ForeignKeyConstraint(["tool_type_id"], ["tool_types.id"]),
        sa.ForeignKeyConstraint(["project_id"], ["projects.id"]),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index("idx_tool_configs_user_tool", "tool_configs", ["user_id", "tool_type_id"])
    op.create_index("idx_tool_configs_project", "tool_configs", ["project_id"])


def downgrade() -> None:
    op.drop_index("idx_tool_configs_project", table_name="tool_configs")
    op.drop_index("idx_tool_configs_user_tool", table_name="tool_configs")
    op.drop_table("tool_configs")
