"""add application_logs table for hybrid logging

Revision ID: 0bd9ffaaced1
Revises: 7a7ce92593d5
Create Date: 2025-11-29 12:44:55.427245

"""
from collections.abc import Sequence

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "0bd9ffaaced1"
down_revision: str | None = "7a7ce92593d5"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    # Create application_logs table
    op.create_table(
        "application_logs",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("timestamp", sa.DateTime(), nullable=False),
        sa.Column("level", sa.String(length=20), nullable=False),
        sa.Column("logger_name", sa.String(length=200), nullable=False),
        sa.Column("module", sa.String(length=200), nullable=True),
        sa.Column("function_name", sa.String(length=100), nullable=True),
        sa.Column("line_number", sa.Integer(), nullable=True),
        sa.Column("message", sa.Text(), nullable=False),
        sa.Column("exception_type", sa.String(length=200), nullable=True),
        sa.Column("exception_message", sa.Text(), nullable=True),
        sa.Column("stack_trace", sa.Text(), nullable=True),
        sa.Column("request_id", sa.String(length=100), nullable=True),
        sa.Column("user_id", sa.Integer(), nullable=True),
        sa.Column("vendor_id", sa.Integer(), nullable=True),
        sa.Column("context", sa.JSON(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(["user_id"], ["users.id"], ),
        sa.ForeignKeyConstraint(["vendor_id"], ["vendors.id"], ),
        sa.PrimaryKeyConstraint("id")
    )

    # Create indexes for better query performance
    op.create_index(op.f("ix_application_logs_id"), "application_logs", ["id"], unique=False)
    op.create_index(op.f("ix_application_logs_timestamp"), "application_logs", ["timestamp"], unique=False)
    op.create_index(op.f("ix_application_logs_level"), "application_logs", ["level"], unique=False)
    op.create_index(op.f("ix_application_logs_logger_name"), "application_logs", ["logger_name"], unique=False)
    op.create_index(op.f("ix_application_logs_request_id"), "application_logs", ["request_id"], unique=False)
    op.create_index(op.f("ix_application_logs_user_id"), "application_logs", ["user_id"], unique=False)
    op.create_index(op.f("ix_application_logs_vendor_id"), "application_logs", ["vendor_id"], unique=False)


def downgrade() -> None:
    # Drop indexes
    op.drop_index(op.f("ix_application_logs_vendor_id"), table_name="application_logs")
    op.drop_index(op.f("ix_application_logs_user_id"), table_name="application_logs")
    op.drop_index(op.f("ix_application_logs_request_id"), table_name="application_logs")
    op.drop_index(op.f("ix_application_logs_logger_name"), table_name="application_logs")
    op.drop_index(op.f("ix_application_logs_level"), table_name="application_logs")
    op.drop_index(op.f("ix_application_logs_timestamp"), table_name="application_logs")
    op.drop_index(op.f("ix_application_logs_id"), table_name="application_logs")

    # Drop table
    op.drop_table("application_logs")
