"""create c2_config and c2_task tables

Revision ID: 0006
Revises: 0005
Create Date: 2026-06-10 00:00:00.000000
"""
import sqlalchemy as sa
from alembic import op

revision = "0006"
down_revision = "0005"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "c2_config",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column(
            "engagement_id",
            sa.Integer(),
            sa.ForeignKey("engagements.id", ondelete="CASCADE"),
            nullable=False,
            unique=True,
        ),
        sa.Column("url", sa.Text(), nullable=False),
        sa.Column("api_token_encrypted", sa.Text(), nullable=False),
        sa.Column("verify_tls", sa.Boolean(), nullable=False, server_default=sa.true()),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
    )
    op.create_index("ix_c2_config_engagement_id", "c2_config", ["engagement_id"])

    op.create_table(
        "c2_task",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column(
            "simulation_id",
            sa.Integer(),
            sa.ForeignKey("simulations.id", ondelete="CASCADE"),
            nullable=False,
            index=True,
        ),
        sa.Column("mythic_task_display_id", sa.Integer(), nullable=False),
        sa.Column("callback_display_id", sa.Integer(), nullable=False),
        sa.Column("command", sa.Text(), nullable=False),
        sa.Column("params", sa.Text(), nullable=True),
        sa.Column("status", sa.Text(), nullable=False),
        sa.Column("completed", sa.Boolean(), nullable=False, server_default=sa.false()),
        sa.Column("output", sa.Text(), nullable=True),
        sa.Column(
            "source",
            sa.Enum("mimic", "import", name="c2task_source"),
            nullable=False,
        ),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("completed_at", sa.DateTime(), nullable=True),
    )


def downgrade() -> None:
    op.drop_table("c2_task")
    op.drop_index("ix_c2_config_engagement_id", "c2_config")
    op.drop_table("c2_config")
    # Remove the enum type (no-op on SQLite, required on Postgres)
    sa.Enum(name="c2task_source").drop(op.get_bind(), checkfirst=True)
