"""v0.10.4 migration

Revision ID: cde44b1247dc
Revises: 4ac2cc4f4e41
Create Date: 2025-05-03 15:38:19.651488

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.engine.reflection import Inspector

# revision identifiers, used by Alembic.
revision: str = "cde44b1247dc"
down_revision: Union[str, None] = "4ac2cc4f4e41"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # Get database connection and inspect constraints
    conn = op.get_bind()
    inspector = Inspector.from_engine(conn)

    # Inspect constraints on the 'activities' table
    constraints_activities = inspector.get_unique_constraints('activities')
    for constraint in constraints_activities:
        if constraint['name'] == 'activities_garminconnect_activity_id_key':
            op.drop_constraint(constraint['name'], 'activities', type_='unique')
            break

    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column(
        "users",
        "gender",
        existing_type=sa.INTEGER(),
        comment="User gender (one digit)(1 - male, 2 - female, 3 - unspecified)",
        existing_comment="User gender (one digit)(1 - male, 2 - female)",
        existing_nullable=False,
    )
    op.add_column(
        "users_integrations",
        sa.Column(
            "strava_client_id", sa.Integer(), nullable=True, comment="Strava client ID"
        ),
    )
    op.add_column(
        "users_integrations",
        sa.Column(
            "strava_client_secret",
            sa.String(length=250),
            nullable=True,
            comment="Strava client secret",
        ),
    )
    op.alter_column(
        "users_integrations",
        "strava_state",
        existing_type=sa.VARCHAR(length=45),
        comment="Strava temporary state for link process",
        existing_nullable=True,
    )
    op.alter_column(
        "users_integrations",
        "strava_token",
        existing_type=sa.VARCHAR(length=250),
        comment="Strava token after link process",
        existing_nullable=True,
    )
    op.alter_column(
        "users_integrations",
        "strava_refresh_token",
        existing_type=sa.VARCHAR(length=250),
        comment="Strava refresh token after link process",
        existing_nullable=True,
    )
    op.alter_column(
        "users_integrations",
        "strava_token_expires_at",
        existing_type=sa.DateTime(),
        comment="Strava token expiration date",
        existing_nullable=True,
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column(
        "users_integrations",
        "strava_token_expires_at",
        existing_type=sa.DateTime(),
        comment=None,
        existing_comment="Strava token expiration date",
        existing_nullable=True,
    )
    op.alter_column(
        "users_integrations",
        "strava_refresh_token",
        existing_type=sa.VARCHAR(length=250),
        comment=None,
        existing_comment="Strava refresh token after link process",
        existing_nullable=True,
    )
    op.alter_column(
        "users_integrations",
        "strava_token",
        existing_type=sa.VARCHAR(length=250),
        comment=None,
        existing_comment="Strava token after link process",
        existing_nullable=True,
    )
    op.alter_column(
        "users_integrations",
        "strava_state",
        existing_type=sa.VARCHAR(length=45),
        comment=None,
        existing_comment="Strava temporary state for link process",
        existing_nullable=True,
    )
    op.drop_column("users_integrations", "strava_client_secret")
    op.drop_column("users_integrations", "strava_client_id")
    op.alter_column(
        "users",
        "gender",
        existing_type=sa.INTEGER(),
        comment="User gender (one digit)(1 - male, 2 - female)",
        existing_comment="User gender (one digit)(1 - male, 2 - female, 3 - unspecified)",
        existing_nullable=False,
    )
    op.create_unique_constraint(
        "activities_garminconnect_activity_id_key",
        "activities",
        ["garminconnect_activity_id"],
    )
    # ### end Alembic commands ###
