"""v0.5.0 migration

Revision ID: 1287b0e32297
Revises: ab815ee3beae
Create Date: 2024-10-17 21:47:04.420602

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision: str = '1287b0e32297'
down_revision: Union[str, None] = 'ab815ee3beae'
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_table('health_data',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False, comment='User ID that the health_data belongs'),
    sa.Column('created_at', sa.Date(), nullable=False, comment='Health data creation date (datetime)'),
    sa.Column('weight', sa.DECIMAL(precision=10, scale=2), nullable=True, comment='Weight in kg'),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('created_at')
    )
    op.create_index(op.f('ix_health_data_user_id'), 'health_data', ['user_id'], unique=False)
    op.create_table('health_targets',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False, comment='User ID that the health_target belongs'),
    sa.Column('weight', sa.DECIMAL(precision=10, scale=2), nullable=True, comment='Weight in kg'),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_health_targets_user_id'), 'health_targets', ['user_id'], unique=True)
    op.add_column('activities', sa.Column('total_elapsed_time', sa.DECIMAL(precision=20, scale=10), nullable=False, comment='Activity total elapsed time (datetime)'))
    op.add_column('activities', sa.Column('total_timer_time', sa.DECIMAL(precision=20, scale=10), nullable=False, comment='Activity total timer time (datetime)'))
    op.add_column('activities', sa.Column('max_speed', sa.DECIMAL(precision=20, scale=10), nullable=True, comment='Max speed seconds per meter (s/m)'))
    op.add_column('activities', sa.Column('max_power', sa.Integer(), nullable=True, comment='Max power (watts)'))
    op.add_column('activities', sa.Column('normalized_power', sa.Integer(), nullable=True, comment='Normalized power (watts)'))
    op.add_column('activities', sa.Column('average_hr', sa.Integer(), nullable=True, comment='Average heart rate (bpm)'))
    op.add_column('activities', sa.Column('max_hr', sa.Integer(), nullable=True, comment='Max heart rate (bpm)'))
    op.add_column('activities', sa.Column('average_cad', sa.Integer(), nullable=True, comment='Average cadence (rpm)'))
    op.add_column('activities', sa.Column('max_cad', sa.Integer(), nullable=True, comment='Max cadence (rpm)'))
    op.add_column('activities', sa.Column('workout_feeling', sa.Integer(), nullable=True, comment='Workout feeling (0 to 100)'))
    op.add_column('activities', sa.Column('workout_rpe', sa.Integer(), nullable=True, comment='Workout RPE (10 to 100)'))
    op.alter_column('activities', 'elevation_gain',
               existing_type=sa.INTEGER(),
               nullable=True,
               existing_comment='Elevation gain in meters')
    op.alter_column('activities', 'elevation_loss',
               existing_type=sa.INTEGER(),
               nullable=True,
               existing_comment='Elevation loss in meters')
    op.alter_column('activities', 'pace',
               existing_type=sa.DECIMAL(precision=20, scale=10),
               nullable=True,
               existing_comment='Pace seconds per meter (s/m)')
    op.alter_column('activities', 'average_speed',
               existing_type=sa.DECIMAL(precision=20, scale=10),
               nullable=True,
               existing_comment='Average speed seconds per meter (s/m)')
    op.alter_column('activities', 'average_power',
               existing_type=sa.INTEGER(),
               nullable=True,
               existing_comment='Average power (watts)')
    op.add_column('users', sa.Column('height', sa.Integer(), nullable=True, comment='User height in centimeters'))
    op.create_table('migrations',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=250), nullable=False, comment='Migration name'),
    sa.Column('description', sa.String(length=2500), nullable=False, comment='Migration description'),
    sa.Column('executed', sa.Boolean(), nullable=False, comment='Whether the migration was executed or not'),
    sa.PrimaryKeyConstraint('id')
    )
    op.execute("""
    INSERT INTO migrations (id, name, description, executed) VALUES
    (1, 'v0.5.0', 'Process additional activity fields for existing activities', false);
    """)
    # Update users preferred_language to 'us'
    op.execute(
        sa.text(
            """
            UPDATE users
            SET preferred_language = 'us'
            """
        )
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('users', 'height')
    op.alter_column('activities', 'average_power',
               existing_type=sa.INTEGER(),
               nullable=False,
               existing_comment='Average power (watts)')
    op.alter_column('activities', 'average_speed',
               existing_type=sa.DECIMAL(precision=20, scale=10),
               nullable=False,
               existing_comment='Average speed seconds per meter (s/m)')
    op.alter_column('activities', 'pace',
               existing_type=sa.DECIMAL(precision=20, scale=10),
               nullable=False,
               existing_comment='Pace seconds per meter (s/m)')
    op.alter_column('activities', 'elevation_loss',
               existing_type=sa.INTEGER(),
               nullable=False,
               existing_comment='Elevation loss in meters')
    op.alter_column('activities', 'elevation_gain',
               existing_type=sa.INTEGER(),
               nullable=False,
               existing_comment='Elevation gain in meters')
    op.drop_column('activities', 'workout_rpe')
    op.drop_column('activities', 'workout_feeling')
    op.drop_column('activities', 'max_cad')
    op.drop_column('activities', 'average_cad')
    op.drop_column('activities', 'max_hr')
    op.drop_column('activities', 'average_hr')
    op.drop_column('activities', 'normalized_power')
    op.drop_column('activities', 'max_power')
    op.drop_column('activities', 'max_speed')
    op.drop_column('activities', 'total_timer_time')
    op.drop_column('activities', 'total_elapsed_time')
    op.drop_index(op.f('ix_health_targets_user_id'), table_name='health_targets')
    op.drop_table('health_targets')
    op.drop_index(op.f('ix_health_data_user_id'), table_name='health_data')
    op.drop_table('health_data')
    op.drop_table('migrations')
    op.execute("""
    DELETE FROM migrations 
    WHERE id = 1;
    """)
    # Update users preferred_language to original 'en' value
    op.execute(
        sa.text(
            """
            UPDATE users
            SET preferred_language = 'en'
            """
        )
    )
    # ### end Alembic commands ###
