"""v0.12.0 migration

Revision ID: f2395550aab9
Revises: 093d3fd32846
Create Date: 2025-06-03 11:24:27.603414

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.sql import text

# revision identifiers, used by Alembic.
revision: str = "f2395550aab9"
down_revision: Union[str, None] = "093d3fd32846"
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(
        "users_privacy_settings",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column(
            "user_id",
            sa.Integer(),
            nullable=False,
            comment="User ID that the privacy settings belongs",
        ),
        sa.Column(
            "default_activity_visibility",
            sa.Integer(),
            nullable=False,
            comment="0 - public, 1 - followers, 2 - private",
        ),
        sa.Column(
            "hide_activity_start_time",
            sa.Boolean(),
            nullable=False,
            comment="Hide activity start time",
        ),
        sa.Column(
            "hide_activity_location",
            sa.Boolean(),
            nullable=False,
            comment="Hide activity location",
        ),
        sa.Column(
            "hide_activity_map",
            sa.Boolean(),
            nullable=False,
            comment="Hide activity map",
        ),
        sa.Column(
            "hide_activity_hr",
            sa.Boolean(),
            nullable=False,
            comment="Hide activity heart rate",
        ),
        sa.Column(
            "hide_activity_power",
            sa.Boolean(),
            nullable=False,
            comment="Hide activity power",
        ),
        sa.Column(
            "hide_activity_cadence",
            sa.Boolean(),
            nullable=False,
            comment="Hide activity cadence",
        ),
        sa.Column(
            "hide_activity_elevation",
            sa.Boolean(),
            nullable=False,
            comment="Hide activity elevation",
        ),
        sa.Column(
            "hide_activity_speed",
            sa.Boolean(),
            nullable=False,
            comment="Hide activity speed",
        ),
        sa.Column(
            "hide_activity_pace",
            sa.Boolean(),
            nullable=False,
            comment="Hide activity pace",
        ),
        sa.Column(
            "hide_activity_laps",
            sa.Boolean(),
            nullable=False,
            comment="Hide activity laps",
        ),
        sa.Column(
            "hide_activity_workout_sets_steps",
            sa.Boolean(),
            nullable=False,
            comment="Hide activity workout sets and steps",
        ),
        sa.Column(
            "hide_activity_gear",
            sa.Boolean(),
            nullable=False,
            comment="Hide activity gear",
        ),
        sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_users_privacy_settings_user_id"),
        "users_privacy_settings",
        ["user_id"],
        unique=False,
    )
    # Insert a default row for each user
    connection = op.get_bind()
    connection.execute(
        text(
            """
        INSERT INTO users_privacy_settings (
            user_id, 
            default_activity_visibility, 
            hide_activity_start_time, 
            hide_activity_location, 
            hide_activity_map, 
            hide_activity_hr, 
            hide_activity_power, 
            hide_activity_cadence, 
            hide_activity_elevation, 
            hide_activity_speed,
            hide_activity_pace,
            hide_activity_laps,
            hide_activity_workout_sets_steps,
            hide_activity_gear
        ) 
        SELECT 
            id, 
            0, 
            FALSE, 
            FALSE, 
            FALSE, 
            FALSE, 
            FALSE, 
            FALSE, 
            FALSE, 
            FALSE,
            FALSE,
            FALSE,
            FALSE,
            FALSE
        FROM users;
    """
        )
    )
    # Copy existing default_activity_visibility values from users table
    connection.execute(
        text(
            "UPDATE users_privacy_settings SET default_activity_visibility = (SELECT default_activity_visibility FROM users WHERE users.id = users_privacy_settings.user_id);"
        )
    )
    op.drop_column("users", "default_activity_visibility")
    op.alter_column(
        "users_integrations",
        "strava_state",
        existing_type=sa.VARCHAR(length=512),
        type_=sa.String(length=45),
        existing_comment="Strava temporary state for link process",
        existing_nullable=True,
    )
    op.add_column(
        "activities",
        sa.Column(
            "hide_start_time",
            sa.Boolean(),
            nullable=True,
            comment="Hide activity start time",
        ),
    )
    op.add_column(
        "activities",
        sa.Column(
            "hide_location",
            sa.Boolean(),
            nullable=True,
            comment="Hide activity location",
        ),
    )
    op.add_column(
        "activities",
        sa.Column("hide_map", sa.Boolean(), nullable=True, comment="Hide activity map"),
    )
    op.add_column(
        "activities",
        sa.Column(
            "hide_hr", sa.Boolean(), nullable=True, comment="Hide activity heart rate"
        ),
    )
    op.add_column(
        "activities",
        sa.Column(
            "hide_power", sa.Boolean(), nullable=True, comment="Hide activity power"
        ),
    )
    op.add_column(
        "activities",
        sa.Column(
            "hide_cadence", sa.Boolean(), nullable=True, comment="Hide activity cadence"
        ),
    )
    op.add_column(
        "activities",
        sa.Column(
            "hide_elevation",
            sa.Boolean(),
            nullable=True,
            comment="Hide activity elevation",
        ),
    )
    op.add_column(
        "activities",
        sa.Column(
            "hide_speed", sa.Boolean(), nullable=True, comment="Hide activity speed"
        ),
    )
    op.add_column(
        "activities",
        sa.Column(
            "hide_pace", sa.Boolean(), nullable=True, comment="Hide activity pace"
        ),
    )
    op.add_column(
        "activities",
        sa.Column(
            "hide_laps", sa.Boolean(), nullable=True, comment="Hide activity laps"
        ),
    )
    op.add_column(
        "activities",
        sa.Column(
            "hide_workout_sets_steps",
            sa.Boolean(),
            nullable=True,
            comment="Hide activity workout sets and steps",
        ),
    )
    op.add_column(
        "activities",
        sa.Column(
            "hide_gear", sa.Boolean(), nullable=True, comment="Hide activity gear"
        ),
    )

    # Update all existing activities to set these columns to FALSE
    connection.execute(
        text(
            """
            UPDATE activities SET 
                hide_start_time = FALSE,
                hide_location = FALSE,
                hide_map = FALSE,
                hide_hr = FALSE,
                hide_power = FALSE,
                hide_cadence = FALSE,
                hide_elevation = FALSE,
                hide_speed = FALSE,
                hide_pace = FALSE,
                hide_laps = FALSE,
                hide_workout_sets_steps = FALSE,
                hide_gear = FALSE
            WHERE hide_start_time IS NULL;
            """
        )
    )

    # Update the new columns to be NOT NULL
    op.alter_column(
        "activities", "hide_start_time", existing_type=sa.Boolean(), nullable=False
    )
    op.alter_column(
        "activities", "hide_location", existing_type=sa.Boolean(), nullable=False
    )
    op.alter_column(
        "activities", "hide_map", existing_type=sa.Boolean(), nullable=False
    )
    op.alter_column("activities", "hide_hr", existing_type=sa.Boolean(), nullable=False)
    op.alter_column(
        "activities", "hide_power", existing_type=sa.Boolean(), nullable=False
    )
    op.alter_column(
        "activities", "hide_cadence", existing_type=sa.Boolean(), nullable=False
    )
    op.alter_column(
        "activities", "hide_elevation", existing_type=sa.Boolean(), nullable=False
    )
    op.alter_column(
        "activities", "hide_speed", existing_type=sa.Boolean(), nullable=False
    )
    op.alter_column(
        "activities", "hide_pace", existing_type=sa.Boolean(), nullable=False
    )
    op.alter_column(
        "activities", "hide_laps", existing_type=sa.Boolean(), nullable=False
    )
    op.alter_column(
        "activities",
        "hide_workout_sets_steps",
        existing_type=sa.Boolean(),
        nullable=False,
    )
    op.alter_column(
        "activities", "hide_gear", existing_type=sa.Boolean(), nullable=False
    )
    # Fix unique index on gear nickname
    op.drop_index(op.f('ix_gear_nickname'), table_name='gear')
    op.create_index(op.f('ix_gear_nickname'), 'gear', ['nickname'], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_gear_nickname'), table_name='gear')
    op.create_index(op.f('ix_gear_nickname'), 'gear', ['nickname'], unique=True)
    op.drop_column("activities", "hide_gear")
    op.drop_column("activities", "hide_workout_sets_steps")
    op.drop_column("activities", "hide_laps")
    op.drop_column("activities", "hide_pace")
    op.drop_column("activities", "hide_speed")
    op.drop_column("activities", "hide_elevation")
    op.drop_column("activities", "hide_cadence")
    op.drop_column("activities", "hide_power")
    op.drop_column("activities", "hide_hr")
    op.drop_column("activities", "hide_map")
    op.drop_column("activities", "hide_location")
    op.drop_column("activities", "hide_start_time")
    op.alter_column(
        "users_integrations",
        "strava_state",
        existing_type=sa.String(length=45),
        type_=sa.VARCHAR(length=512),
        existing_comment="Strava temporary state for link process",
        existing_nullable=True,
    )
    op.add_column(
        "users",
        sa.Column(
            "default_activity_visibility",
            sa.INTEGER(),
            server_default=sa.text("0"),
            autoincrement=False,
            nullable=False,
            comment="0 - public, 1 - followers, 2 - private",
        ),
    )
    # Copy existing default_activity_visibility values from users_privacy_settings table
    connection = op.get_bind()
    connection.execute(
        text(
            "UPDATE users SET default_activity_visibility = (SELECT default_activity_visibility FROM users_privacy_settings WHERE users_privacy_settings.user_id = users.id);"
        )
    )
    op.drop_index(
        op.f("ix_users_privacy_settings_user_id"), table_name="users_privacy_settings"
    )
    op.drop_table("users_privacy_settings")
    # ### end Alembic commands ###
