"""Add lab test components table

Revision ID: 442af1bdd4b8
Revises: 64e9f37c106e
Create Date: 2025-09-30 07:35:49.107100

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = '442af1bdd4b8'
down_revision = '64e9f37c106e'
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('lab_test_components',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('lab_result_id', sa.Integer(), nullable=False),
    sa.Column('test_name', sa.String(), nullable=False),
    sa.Column('abbreviation', sa.String(), nullable=True),
    sa.Column('test_code', sa.String(), nullable=True),
    sa.Column('value', sa.Float(), nullable=False),
    sa.Column('unit', sa.String(), nullable=False),
    sa.Column('ref_range_min', sa.Float(), nullable=True),
    sa.Column('ref_range_max', sa.Float(), nullable=True),
    sa.Column('ref_range_text', sa.String(), nullable=True),
    sa.Column('status', sa.String(), nullable=True),
    sa.Column('category', sa.String(), nullable=True),
    sa.Column('display_order', sa.Integer(), nullable=True),
    sa.Column('notes', sa.Text(), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['lab_result_id'], ['lab_results.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('idx_lab_test_components_category', 'lab_test_components', ['category'], unique=False)
    op.create_index('idx_lab_test_components_lab_result_id', 'lab_test_components', ['lab_result_id'], unique=False)
    op.create_index('idx_lab_test_components_status', 'lab_test_components', ['status'], unique=False)
    op.drop_index('idx_allergies_tags', table_name='allergies')
    op.drop_index('idx_conditions_tags', table_name='conditions')
    op.drop_index('idx_encounters_tags', table_name='encounters')
    op.drop_index('idx_immunizations_tags', table_name='immunizations')
    op.drop_index('idx_lab_results_tags', table_name='lab_results')
    op.drop_index('idx_medications_tags', table_name='medications')
    op.drop_index('idx_procedures_tags', table_name='procedures')
    op.alter_column('report_generation_audit', 'status',
               existing_type=sa.VARCHAR(length=20),
               server_default=None,
               existing_nullable=False)
    op.alter_column('report_templates', 'report_settings',
               existing_type=postgresql.JSONB(astext_type=sa.Text()),
               server_default=None,
               existing_nullable=False)
    op.alter_column('report_templates', 'is_public',
               existing_type=sa.BOOLEAN(),
               server_default=None,
               existing_nullable=False)
    op.alter_column('report_templates', 'shared_with_family',
               existing_type=sa.BOOLEAN(),
               server_default=None,
               existing_nullable=False)
    op.alter_column('report_templates', 'is_active',
               existing_type=sa.BOOLEAN(),
               server_default=None,
               existing_nullable=False)
    op.drop_index('idx_treatments_tags', table_name='treatments')
    op.alter_column('users', 'auth_method',
               existing_type=sa.VARCHAR(length=20),
               server_default=None,
               existing_nullable=False)
    op.drop_index('idx_users_auth_method', table_name='users')
    op.drop_index('idx_users_external_id', table_name='users')
    op.drop_index('idx_users_sso_provider', table_name='users')
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_index('idx_users_sso_provider', 'users', ['sso_provider'], unique=False)
    op.create_index('idx_users_external_id', 'users', ['external_id'], unique=False)
    op.create_index('idx_users_auth_method', 'users', ['auth_method'], unique=False)
    op.alter_column('users', 'auth_method',
               existing_type=sa.VARCHAR(length=20),
               server_default=sa.text("'local'::character varying"),
               existing_nullable=False)
    op.create_index('idx_treatments_tags', 'treatments', ['tags'], unique=False)
    op.alter_column('report_templates', 'is_active',
               existing_type=sa.BOOLEAN(),
               server_default=sa.text('true'),
               existing_nullable=False)
    op.alter_column('report_templates', 'shared_with_family',
               existing_type=sa.BOOLEAN(),
               server_default=sa.text('false'),
               existing_nullable=False)
    op.alter_column('report_templates', 'is_public',
               existing_type=sa.BOOLEAN(),
               server_default=sa.text('false'),
               existing_nullable=False)
    op.alter_column('report_templates', 'report_settings',
               existing_type=postgresql.JSONB(astext_type=sa.Text()),
               server_default=sa.text("'{}'::jsonb"),
               existing_nullable=False)
    op.alter_column('report_generation_audit', 'status',
               existing_type=sa.VARCHAR(length=20),
               server_default=sa.text("'success'::character varying"),
               existing_nullable=False)
    op.create_index('idx_procedures_tags', 'procedures', ['tags'], unique=False)
    op.create_index('idx_medications_tags', 'medications', ['tags'], unique=False)
    op.create_index('idx_lab_results_tags', 'lab_results', ['tags'], unique=False)
    op.create_index('idx_immunizations_tags', 'immunizations', ['tags'], unique=False)
    op.create_index('idx_encounters_tags', 'encounters', ['tags'], unique=False)
    op.create_index('idx_conditions_tags', 'conditions', ['tags'], unique=False)
    op.create_index('idx_allergies_tags', 'allergies', ['tags'], unique=False)
    op.drop_index('idx_lab_test_components_status', table_name='lab_test_components')
    op.drop_index('idx_lab_test_components_lab_result_id', table_name='lab_test_components')
    op.drop_index('idx_lab_test_components_category', table_name='lab_test_components')
    op.drop_table('lab_test_components')
    # ### end Alembic commands ###
