"""add_tests_table

Revision ID: 14c6f07b47cb
Revises: 476930a8d86e
Create Date: 2026-02-06 11:01:01.822240

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


def upgrade() -> None:
    """Upgrade schema."""
    # Create enum types
    testresult_enum = postgresql.ENUM('detected', 'not_detected', 'partially_detected', name='testresult')
    teststate_enum = postgresql.ENUM('draft', 'in_review', 'validated', 'rejected', name='teststate')
    testresult_enum.create(op.get_bind(), checkfirst=True)
    teststate_enum.create(op.get_bind(), checkfirst=True)
    
    # Create table
    op.create_table('tests',
    sa.Column('id', sa.UUID(), nullable=False),
    sa.Column('technique_id', sa.UUID(), nullable=False),
    sa.Column('name', sa.String(), nullable=False),
    sa.Column('description', sa.Text(), nullable=True),
    sa.Column('platform', sa.String(), nullable=True),
    sa.Column('procedure_text', sa.Text(), nullable=True),
    sa.Column('tool_used', sa.String(), nullable=True),
    sa.Column('execution_date', sa.DateTime(), nullable=True),
    sa.Column('created_by', sa.UUID(), nullable=True),
    sa.Column('result', postgresql.ENUM('detected', 'not_detected', 'partially_detected', name='testresult', create_type=False), nullable=True),
    sa.Column('state', postgresql.ENUM('draft', 'in_review', 'validated', 'rejected', name='teststate', create_type=False), nullable=True),
    sa.Column('validated_by', sa.UUID(), nullable=True),
    sa.Column('validated_at', sa.DateTime(), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=True),
    sa.ForeignKeyConstraint(['created_by'], ['users.id'], ),
    sa.ForeignKeyConstraint(['technique_id'], ['techniques.id'], ),
    sa.ForeignKeyConstraint(['validated_by'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )


def downgrade() -> None:
    """Downgrade schema."""
    op.drop_table('tests')
    # Drop enum types
    postgresql.ENUM(name='testresult').drop(op.get_bind(), checkfirst=True)
    postgresql.ENUM(name='teststate').drop(op.get_bind(), checkfirst=True)
