"""standardize_to_snake_case

Revision ID: 80d4c7cb87be
Revises: c412d18de747
Create Date: 2025-07-02 19:55:03.746769

"""

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision = "80d4c7cb87be"
down_revision = "c412d18de747"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###

    # Conditions table changes
    op.add_column("conditions", sa.Column("onset_date", sa.Date(), nullable=True))
    op.add_column("conditions", sa.Column("end_date", sa.Date(), nullable=True))
    op.execute('UPDATE conditions SET onset_date = "onsetDate", end_date = "endDate"')
    op.drop_column("conditions", "endDate")
    op.drop_column("conditions", "onsetDate")

    # Medications table changes
    op.add_column(
        "medications", sa.Column("effective_period_start", sa.Date(), nullable=True)
    )
    op.add_column(
        "medications", sa.Column("effective_period_end", sa.Date(), nullable=True)
    )
    op.execute(
        'UPDATE medications SET effective_period_start = "effectivePeriod_start", effective_period_end = "effectivePeriod_end"'
    )
    op.drop_column("medications", "effectivePeriod_start")
    op.drop_column("medications", "effectivePeriod_end")

    # Patients table changes
    op.add_column("patients", sa.Column("birth_date", sa.Date(), nullable=True))
    op.add_column("patients", sa.Column("blood_type", sa.String(), nullable=True))
    op.execute('UPDATE patients SET birth_date = "birthDate", blood_type = "bloodType"')
    op.alter_column("patients", "birth_date", existing_type=sa.Date(), nullable=False)
    op.drop_column("patients", "bloodType")
    op.drop_column("patients", "birthDate")
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "patients",
        sa.Column("birthDate", sa.DATE(), autoincrement=False, nullable=False),
    )
    op.add_column(
        "patients",
        sa.Column("bloodType", sa.VARCHAR(), autoincrement=False, nullable=True),
    )
    op.execute('UPDATE patients SET "birthDate" = birth_date, "bloodType" = blood_type')
    op.drop_column("patients", "blood_type")
    op.drop_column("patients", "birth_date")

    op.add_column(
        "medications",
        sa.Column("effectivePeriod_end", sa.DATE(), autoincrement=False, nullable=True),
    )
    op.add_column(
        "medications",
        sa.Column(
            "effectivePeriod_start", sa.DATE(), autoincrement=False, nullable=True
        ),
    )
    op.execute(
        'UPDATE medications SET "effectivePeriod_start" = effective_period_start, "effectivePeriod_end" = effective_period_end'
    )
    op.drop_column("medications", "effective_period_end")
    op.drop_column("medications", "effective_period_start")

    op.add_column(
        "conditions",
        sa.Column("onsetDate", sa.DATE(), autoincrement=False, nullable=True),
    )
    op.add_column(
        "conditions",
        sa.Column("endDate", sa.DATE(), autoincrement=False, nullable=True),
    )
    op.execute('UPDATE conditions SET "onsetDate" = onset_date, "endDate" = end_date')
    op.drop_column("conditions", "end_date")
    op.drop_column("conditions", "onset_date")
    # ### end Alembic commands ###
