"""add pico column to review_literatures

Revision ID: f80dca5baa02
Revises: 5f20d93a7606
Create Date: 2026-07-08 15:43:32.898597
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

revision: str = 'f80dca5baa02'
down_revision: Union[str, None] = '5f20d93a7606'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_foreign_key(None, 'api_keys', 'tenants', ['tenant_id'], ['id'], ondelete='CASCADE')
    op.create_foreign_key(None, 'api_keys', 'users', ['user_id'], ['id'], ondelete='CASCADE')
    op.create_foreign_key(None, 'api_usage_logs', 'tenants', ['tenant_id'], ['id'], ondelete='CASCADE')
    op.create_foreign_key(None, 'api_usage_logs', 'api_keys', ['api_key_id'], ['id'], ondelete='SET NULL')
    op.drop_constraint(op.f('approval_steps_approver_id_fkey'), 'approval_steps', type_='foreignkey')
    op.create_foreign_key(None, 'approval_steps', 'users', ['approver_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint(op.f('approval_workflows_submitted_by_fkey'), 'approval_workflows', type_='foreignkey')
    op.create_foreign_key(None, 'approval_workflows', 'users', ['submitted_by'], ['id'], ondelete='CASCADE')
    op.drop_constraint(op.f('drug_approvals_cancer_type_tag_id_fkey'), 'drug_approvals', type_='foreignkey')
    op.create_foreign_key(None, 'drug_approvals', 'global_tags', ['cancer_type_tag_id'], ['id'], ondelete='SET NULL')
    op.create_foreign_key(None, 'export_jobs', 'users', ['user_id'], ['id'], ondelete='CASCADE')
    op.create_foreign_key(None, 'export_jobs', 'tenants', ['tenant_id'], ['id'], ondelete='CASCADE')
    op.drop_index(op.f('ix_gl_search_tsv'), table_name='global_literature', postgresql_using='gin')
    op.drop_constraint(op.f('guideline_versions_cancer_type_tag_id_fkey'), 'guideline_versions', type_='foreignkey')
    op.create_foreign_key(None, 'guideline_versions', 'global_tags', ['cancer_type_tag_id'], ['id'], ondelete='RESTRICT')
    op.drop_constraint(op.f('invitations_inviter_id_fkey'), 'invitations', type_='foreignkey')
    op.create_foreign_key(None, 'invitations', 'users', ['inviter_id'], ['id'], ondelete='CASCADE')
    op.alter_column('journal_club_queue', 'suggested_by',
               existing_type=sa.UUID(),
               nullable=True)
    op.drop_constraint(op.f('journal_club_queue_suggested_by_fkey'), 'journal_club_queue', type_='foreignkey')
    op.drop_constraint(op.f('journal_club_queue_presenter_id_fkey'), 'journal_club_queue', type_='foreignkey')
    op.create_foreign_key(None, 'journal_club_queue', 'users', ['suggested_by'], ['id'], ondelete='SET NULL')
    op.create_foreign_key(None, 'journal_club_queue', 'users', ['presenter_id'], ['id'], ondelete='SET NULL')
    op.create_foreign_key(None, 'payment_orders', 'users', ['user_id'], ['id'], ondelete='CASCADE')
    op.create_foreign_key(None, 'payment_orders', 'tenants', ['tenant_id'], ['id'], ondelete='CASCADE')
    op.add_column('review_literatures', sa.Column('pico', sa.JSON(), nullable=True))
    op.alter_column('review_literatures', 'quality_detail',
               existing_type=postgresql.JSON(astext_type=sa.Text()),
               nullable=False)
    op.alter_column('review_literatures', 'created_at',
               existing_type=postgresql.TIMESTAMP(timezone=True),
               nullable=False,
               existing_server_default=sa.text('now()'))
    op.alter_column('review_literatures', 'updated_at',
               existing_type=postgresql.TIMESTAMP(timezone=True),
               nullable=False,
               existing_server_default=sa.text('now()'))
    op.alter_column('shared_folders', 'created_by',
               existing_type=sa.UUID(),
               nullable=True)
    op.drop_constraint(op.f('shared_folders_created_by_fkey'), 'shared_folders', type_='foreignkey')
    op.create_foreign_key(None, 'shared_folders', 'users', ['created_by'], ['id'], ondelete='SET NULL')
    op.create_foreign_key(None, 'subscription_events', 'tenants', ['tenant_id'], ['id'], ondelete='CASCADE')
    op.alter_column('systematic_reviews', 'inclusion_criteria',
               existing_type=postgresql.JSON(astext_type=sa.Text()),
               nullable=False)
    op.alter_column('systematic_reviews', 'exclusion_criteria',
               existing_type=postgresql.JSON(astext_type=sa.Text()),
               nullable=False)
    op.alter_column('systematic_reviews', 'databases_searched',
               existing_type=postgresql.JSON(astext_type=sa.Text()),
               nullable=False)
    op.alter_column('systematic_reviews', 'search_strategy',
               existing_type=postgresql.JSON(astext_type=sa.Text()),
               nullable=False)
    op.alter_column('systematic_reviews', 'created_at',
               existing_type=postgresql.TIMESTAMP(timezone=True),
               nullable=False,
               existing_server_default=sa.text('now()'))
    op.alter_column('systematic_reviews', 'updated_at',
               existing_type=postgresql.TIMESTAMP(timezone=True),
               nullable=False,
               existing_server_default=sa.text('now()'))
    op.alter_column('teams', 'created_by',
               existing_type=sa.UUID(),
               nullable=True)
    op.drop_constraint(op.f('teams_created_by_fkey'), 'teams', type_='foreignkey')
    op.create_foreign_key(None, 'teams', 'users', ['created_by'], ['id'], ondelete='SET NULL')
    op.create_foreign_key(None, 'tenant_subscriptions', 'tenants', ['tenant_id'], ['id'], ondelete='CASCADE')
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'tenant_subscriptions', type_='foreignkey')
    op.drop_constraint(None, 'teams', type_='foreignkey')
    op.create_foreign_key(op.f('teams_created_by_fkey'), 'teams', 'users', ['created_by'], ['id'])
    op.alter_column('teams', 'created_by',
               existing_type=sa.UUID(),
               nullable=False)
    op.alter_column('systematic_reviews', 'updated_at',
               existing_type=postgresql.TIMESTAMP(timezone=True),
               nullable=True,
               existing_server_default=sa.text('now()'))
    op.alter_column('systematic_reviews', 'created_at',
               existing_type=postgresql.TIMESTAMP(timezone=True),
               nullable=True,
               existing_server_default=sa.text('now()'))
    op.alter_column('systematic_reviews', 'search_strategy',
               existing_type=postgresql.JSON(astext_type=sa.Text()),
               nullable=True)
    op.alter_column('systematic_reviews', 'databases_searched',
               existing_type=postgresql.JSON(astext_type=sa.Text()),
               nullable=True)
    op.alter_column('systematic_reviews', 'exclusion_criteria',
               existing_type=postgresql.JSON(astext_type=sa.Text()),
               nullable=True)
    op.alter_column('systematic_reviews', 'inclusion_criteria',
               existing_type=postgresql.JSON(astext_type=sa.Text()),
               nullable=True)
    op.drop_constraint(None, 'subscription_events', type_='foreignkey')
    op.drop_constraint(None, 'shared_folders', type_='foreignkey')
    op.create_foreign_key(op.f('shared_folders_created_by_fkey'), 'shared_folders', 'users', ['created_by'], ['id'])
    op.alter_column('shared_folders', 'created_by',
               existing_type=sa.UUID(),
               nullable=False)
    op.alter_column('review_literatures', 'updated_at',
               existing_type=postgresql.TIMESTAMP(timezone=True),
               nullable=True,
               existing_server_default=sa.text('now()'))
    op.alter_column('review_literatures', 'created_at',
               existing_type=postgresql.TIMESTAMP(timezone=True),
               nullable=True,
               existing_server_default=sa.text('now()'))
    op.alter_column('review_literatures', 'quality_detail',
               existing_type=postgresql.JSON(astext_type=sa.Text()),
               nullable=True)
    op.drop_column('review_literatures', 'pico')
    op.drop_constraint(None, 'payment_orders', type_='foreignkey')
    op.drop_constraint(None, 'payment_orders', type_='foreignkey')
    op.drop_constraint(None, 'journal_club_queue', type_='foreignkey')
    op.drop_constraint(None, 'journal_club_queue', type_='foreignkey')
    op.create_foreign_key(op.f('journal_club_queue_presenter_id_fkey'), 'journal_club_queue', 'users', ['presenter_id'], ['id'])
    op.create_foreign_key(op.f('journal_club_queue_suggested_by_fkey'), 'journal_club_queue', 'users', ['suggested_by'], ['id'])
    op.alter_column('journal_club_queue', 'suggested_by',
               existing_type=sa.UUID(),
               nullable=False)
    op.drop_constraint(None, 'invitations', type_='foreignkey')
    op.create_foreign_key(op.f('invitations_inviter_id_fkey'), 'invitations', 'users', ['inviter_id'], ['id'])
    op.drop_constraint(None, 'guideline_versions', type_='foreignkey')
    op.create_foreign_key(op.f('guideline_versions_cancer_type_tag_id_fkey'), 'guideline_versions', 'global_tags', ['cancer_type_tag_id'], ['id'])
    op.create_index(op.f('ix_gl_search_tsv'), 'global_literature', ['search_tsv'], unique=False, postgresql_using='gin')
    op.drop_constraint(None, 'export_jobs', type_='foreignkey')
    op.drop_constraint(None, 'export_jobs', type_='foreignkey')
    op.drop_constraint(None, 'drug_approvals', type_='foreignkey')
    op.create_foreign_key(op.f('drug_approvals_cancer_type_tag_id_fkey'), 'drug_approvals', 'global_tags', ['cancer_type_tag_id'], ['id'])
    op.drop_constraint(None, 'approval_workflows', type_='foreignkey')
    op.create_foreign_key(op.f('approval_workflows_submitted_by_fkey'), 'approval_workflows', 'users', ['submitted_by'], ['id'])
    op.drop_constraint(None, 'approval_steps', type_='foreignkey')
    op.create_foreign_key(op.f('approval_steps_approver_id_fkey'), 'approval_steps', 'users', ['approver_id'], ['id'])
    op.drop_constraint(None, 'api_usage_logs', type_='foreignkey')
    op.drop_constraint(None, 'api_usage_logs', type_='foreignkey')
    op.drop_constraint(None, 'api_keys', type_='foreignkey')
    op.drop_constraint(None, 'api_keys', type_='foreignkey')
    # ### end Alembic commands ###
