"""add systematic_reviews + review_literatures tables

Revision ID: b5c9d3e4f5g6
Revises: a4b8c2d3e5f6
Create Date: 2026-07-07 19:30:00.000000
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa


revision: str = 'b5c9d3e4f5g6'
down_revision: Union[str, None] = 'a4b8c2d3e5f6'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table('systematic_reviews',
        sa.Column('id', sa.Uuid(), nullable=False),
        sa.Column('user_id', sa.Uuid(), nullable=False),
        sa.Column('tenant_id', sa.Uuid(), nullable=False),
        sa.Column('title', sa.String(500), nullable=False),
        sa.Column('objective', sa.Text(), nullable=True),
        sa.Column('protocol', sa.Text(), nullable=True),
        sa.Column('inclusion_criteria', sa.JSON(), nullable=True),
        sa.Column('exclusion_criteria', sa.JSON(), nullable=True),
        sa.Column('databases_searched', sa.JSON(), nullable=True),
        sa.Column('search_strategy', sa.JSON(), nullable=True),
        sa.Column('prisma_stage', sa.String(20), nullable=False, server_default='identification'),
        sa.Column('status', sa.String(20), nullable=False, server_default='draft'),
        sa.Column('notes', sa.Text(), nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.func.now()),
        sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.func.now()),
        sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
        sa.ForeignKeyConstraint(['tenant_id'], ['tenants.id'], ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('id'),
    )
    op.create_table('review_literatures',
        sa.Column('id', sa.Uuid(), nullable=False),
        sa.Column('review_id', sa.Uuid(), nullable=False),
        sa.Column('literature_id', sa.Uuid(), nullable=False),
        sa.Column('stage', sa.String(20), nullable=False, server_default='screening'),
        sa.Column('exclusion_reason', sa.Text(), nullable=True),
        sa.Column('notes', sa.Text(), nullable=True),
        sa.Column('quality_score', sa.String(10), nullable=True),
        sa.Column('quality_detail', sa.JSON(), nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.func.now()),
        sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.func.now()),
        sa.ForeignKeyConstraint(['review_id'], ['systematic_reviews.id'], ondelete='CASCADE'),
        sa.ForeignKeyConstraint(['literature_id'], ['global_literature.id'], ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('id'),
    )


def downgrade() -> None:
    op.drop_table('review_literatures')
    op.drop_table('systematic_reviews')
