"""Add api_asset and api_endpoint tables

Revision ID: 014
Revises: 013
Create Date: 2026-04-24 00:00:00.000000

"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa

revision: str = "014"
down_revision: Union[str, None] = "013"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        "api_asset",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("name", sa.String(200), nullable=False),
        sa.Column("base_url", sa.String(500), nullable=False),
        sa.Column("swagger_url", sa.String(500), nullable=True),
        sa.Column("auth_type", sa.String(50), default="none"),
        sa.Column("headers", sa.JSON(), default=dict),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("scan_status", sa.String(20), default="idle"),
        sa.Column("total_endpoints", sa.Integer(), default=0),
        sa.Column("sensitive_endpoints", sa.Integer(), default=0),
        sa.Column("created_by", sa.Integer(), sa.ForeignKey("sys_user.id"), nullable=True),
        sa.Column("created_at", sa.DateTime(), default=sa.func.now()),
        sa.Column("updated_at", sa.DateTime(), default=sa.func.now(), onupdate=sa.func.now()),
    )
    op.create_table(
        "api_endpoint",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("asset_id", sa.Integer(), sa.ForeignKey("api_asset.id"), nullable=False),
        sa.Column("method", sa.String(10), nullable=False),
        sa.Column("path", sa.String(500), nullable=False),
        sa.Column("summary", sa.String(500), nullable=True),
        sa.Column("tags", sa.JSON(), default=list),
        sa.Column("parameters", sa.JSON(), default=list),
        sa.Column("request_body_schema", sa.JSON(), nullable=True),
        sa.Column("response_schema", sa.JSON(), nullable=True),
        sa.Column("sensitive_fields", sa.JSON(), default=list),
        sa.Column("risk_level", sa.String(20), default="low"),
        sa.Column("is_active", sa.Boolean(), default=True),
        sa.Column("created_at", sa.DateTime(), default=sa.func.now()),
    )


def downgrade() -> None:
    op.drop_table("api_endpoint")
    op.drop_table("api_asset")
