"""add command_history table

Revision ID: 0012_command_history
Revises: 0011_server_hostname
Create Date: 2026-05-14
"""
from __future__ import annotations

from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op


revision: str = "0012_command_history"
down_revision: Union[str, Sequence[str], None] = "0011_server_hostname"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        "command_history",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("user_id", sa.Integer(), sa.ForeignKey("users.id"), nullable=False),
        sa.Column(
            "server_id",
            sa.Integer(),
            sa.ForeignKey("servers.id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column("command", sa.Text(), nullable=False),
        sa.Column("reply", sa.Text(), nullable=False, server_default=""),
        sa.Column(
            "is_error", sa.Boolean(), nullable=False, server_default=sa.text("0")
        ),
        sa.Column("created_at", sa.DateTime(), nullable=False),
    )
    op.create_index(
        "ix_cmdhist_user_server_id",
        "command_history",
        ["user_id", "server_id", "id"],
    )


def downgrade() -> None:
    op.drop_index("ix_cmdhist_user_server_id", table_name="command_history")
    op.drop_table("command_history")
