"""add geometry and points to workout segments

Revision ID: fed271354410
Revises: b0ef6ca5ec92
Create Date: 2025-05-28 10:59:17.126175

"""
from alembic import op
import sqlalchemy as sa
from fittrackee.database import TZDateTime
from geoalchemy2 import Geometry

# revision identifiers, used by Alembic.
revision = 'fed271354410'
down_revision = '8221053f0e81'
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_geospatial_column(sa.Column('geom', Geometry(geometry_type='LINESTRING', srid=4326, dimension=2, spatial_index=False, from_text='ST_GeomFromEWKT', name='geometry'), nullable=True))
        batch_op.add_column(sa.Column('points', sa.JSON(), server_default='[]', nullable=False))
        batch_op.add_column(sa.Column('uuid', sa.UUID(), server_default=sa.text('gen_random_uuid ()'), nullable=False))
        batch_op.add_column(sa.Column('start_date', TZDateTime(), nullable=True))
        batch_op.create_geospatial_index('idx_workout_segments_geom', ['geom'], unique=False, postgresql_using='gist', postgresql_ops={})
        batch_op.create_index(batch_op.f('ix_workout_segments_start_date'), ['start_date'], unique=False)
        batch_op.create_unique_constraint('workout_segments_uuid_key', ['uuid'])
        batch_op.create_unique_constraint('workout_id_start_date_unique', ['workout_id', 'start_date'])

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('workout_segments', schema=None) as batch_op:
        batch_op.drop_constraint('workout_id_start_date_unique', type_='unique')
        batch_op.drop_constraint('workout_segments_uuid_key', type_='unique')
        batch_op.drop_index(batch_op.f('ix_workout_segments_start_date'))
        batch_op.drop_geospatial_index('idx_workout_segments_geom', postgresql_using='gist', column_name='geom')
        batch_op.drop_column('start_date')
        batch_op.drop_column('uuid')
        batch_op.drop_column('points')
        batch_op.drop_geospatial_column('geom')

    # ### end Alembic commands ###
