"""add_user_preferences_table

Revision ID: 2f7e836195bb
Revises: 26dd25579d9f
Create Date: 2025-07-21 16:06:42.182820

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '2f7e836195bb'
down_revision = '26dd25579d9f'
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('user_preferences',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('unit_system', sa.String(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('user_id')
    )
    
    # Create default imperial preferences for all existing users
    connection = op.get_bind()
    connection.execute(sa.text("""
        INSERT INTO user_preferences (user_id, unit_system, created_at, updated_at)
        SELECT id, 'imperial', NOW(), NOW()
        FROM users
    """))
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('user_preferences')
    # ### end Alembic commands ###
