"""add_techniques_table

Revision ID: 476930a8d86e
Revises: 4671fccfa705
Create Date: 2026-02-06 10:37:17.777106

"""
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 = '476930a8d86e'
down_revision: Union[str, Sequence[str], None] = '4671fccfa705'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # Create enum type explicitly
    techniquestatus_enum = postgresql.ENUM(
        'not_evaluated', 'in_progress', 'validated', 'partial', 'not_covered', 'review_required',
        name='techniquestatus'
    )
    techniquestatus_enum.create(op.get_bind(), checkfirst=True)
    
    # Create table with create_type=False since we already created the enum
    op.create_table('techniques',
    sa.Column('id', sa.UUID(), nullable=False),
    sa.Column('mitre_id', sa.String(), nullable=False),
    sa.Column('name', sa.String(), nullable=False),
    sa.Column('description', sa.Text(), nullable=True),
    sa.Column('tactic', sa.String(), nullable=True),
    sa.Column('platforms', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
    sa.Column('mitre_version', sa.String(), nullable=True),
    sa.Column('mitre_last_modified', sa.DateTime(), nullable=True),
    sa.Column('is_subtechnique', sa.Boolean(), nullable=True),
    sa.Column('parent_mitre_id', sa.String(), nullable=True),
    sa.Column('status_global', postgresql.ENUM('not_evaluated', 'in_progress', 'validated', 'partial', 'not_covered', 'review_required', name='techniquestatus', create_type=False), nullable=True),
    sa.Column('review_required', sa.Boolean(), nullable=True),
    sa.Column('last_review_date', sa.DateTime(), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('mitre_id')
    )


def downgrade() -> None:
    """Downgrade schema."""
    op.drop_table('techniques')
    # Drop enum type after dropping table
    postgresql.ENUM(name='techniquestatus').drop(op.get_bind(), checkfirst=True)
