"""add pace to 'workouts' and 'workout_segments' tables

Revision ID: 3dbea67b5e3b
Revises: e63433a1d62e
Create Date: 2025-11-29 12:06:13.083883

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '3dbea67b5e3b'
down_revision = 'e63433a1d62e'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('workout_segments', schema=None) as batch_op:
        batch_op.add_column(sa.Column('ave_pace', sa.Interval(), nullable=True))
        batch_op.add_column(sa.Column('best_pace', sa.Interval(), nullable=True))

    with op.batch_alter_table('workouts', schema=None) as batch_op:
        batch_op.add_column(sa.Column('ave_pace', sa.Interval(), nullable=True))
        batch_op.add_column(sa.Column('best_pace', sa.Interval(), nullable=True))

    # ### end Alembic commands ###

    op.execute("ALTER TYPE record_types ADD VALUE 'AP'")
    op.execute("ALTER TYPE record_types ADD VALUE 'BP'")

    with op.batch_alter_table("users", schema=None) as batch_op:
        batch_op.add_column(
            sa.Column(
                "display_speed_with_pace",
                sa.Boolean(),
                server_default="false",
                nullable=False,
            )
        )


def downgrade():
    with op.batch_alter_table("users", schema=None) as batch_op:
        batch_op.drop_column("display_speed_with_pace")

    op.execute("DELETE FROM records WHERE record_type IN ('AP', 'BP');")
    op.execute("ALTER TYPE record_types RENAME TO record_types_old")
    op.execute("CREATE TYPE record_types AS ENUM('AS', 'FD', 'LD', 'MS', 'HA')")
    op.execute(
        """
        ALTER TABLE records ALTER COLUMN record_type TYPE record_types
        USING record_type::text::record_types
    """
    )
    op.execute("DROP TYPE record_types_old")

    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('workouts', schema=None) as batch_op:
        batch_op.drop_column('best_pace')
        batch_op.drop_column('ave_pace')

    with op.batch_alter_table('workout_segments', schema=None) as batch_op:
        batch_op.drop_column('best_pace')
        batch_op.drop_column('ave_pace')

    # ### end Alembic commands ###
