"""revised_procedure_table_code_and_others

Revision ID: 82683ba6bc3e
Revises: b784b6b63ffa
Create Date: 2025-07-04 13:21:48.856486

"""

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision = "82683ba6bc3e"
down_revision = "b784b6b63ffa"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    # Rename 'code' column to 'procedure_type' and preserve data
    op.add_column("procedures", sa.Column("procedure_type", sa.String(), nullable=True))
    op.execute("UPDATE procedures SET procedure_type = code WHERE code IS NOT NULL")

    # Add new columns
    op.add_column("procedures", sa.Column("procedure_code", sa.String(), nullable=True))
    op.add_column(
        "procedures", sa.Column("procedure_setting", sa.String(), nullable=True)
    )
    op.add_column(
        "procedures", sa.Column("procedure_complications", sa.String(), nullable=True)
    )
    op.add_column(
        "procedures", sa.Column("procedure_duration", sa.Integer(), nullable=True)
    )

    # Now drop the old 'code' column
    op.drop_column("procedures", "code")
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    # Restore 'code' column and preserve data from 'procedure_type'
    op.add_column(
        "procedures",
        sa.Column("code", sa.VARCHAR(), autoincrement=False, nullable=True),
    )
    op.execute(
        "UPDATE procedures SET code = procedure_type WHERE procedure_type IS NOT NULL"
    )

    # Drop the new columns
    op.drop_column("procedures", "procedure_duration")
    op.drop_column("procedures", "procedure_complications")
    op.drop_column("procedures", "procedure_setting")
    op.drop_column("procedures", "procedure_code")
    op.drop_column("procedures", "procedure_type")
    # ### end Alembic commands ###
