"""Add user_pinned_trees table

Revision ID: 037
Revises: 036
Create Date: 2026-02-15

Adds pinned flows feature for sidebar favorites.
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import UUID


# revision identifiers, used by Alembic.
revision: str = '037'
down_revision: Union[str, None] = '036'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        'user_pinned_trees',
        sa.Column('id', UUID(as_uuid=True), primary_key=True, server_default=sa.text('gen_random_uuid()')),
        sa.Column('user_id', UUID(as_uuid=True), sa.ForeignKey('users.id', ondelete='CASCADE'), nullable=False),
        sa.Column('tree_id', UUID(as_uuid=True), sa.ForeignKey('trees.id', ondelete='CASCADE'), nullable=False),
        sa.Column('display_order', sa.Integer(), nullable=False, server_default='0'),
        sa.Column('pinned_at', sa.DateTime(timezone=True), server_default=sa.text('NOW()')),
        sa.UniqueConstraint('user_id', 'tree_id', name='uq_user_pinned_tree'),
    )
    op.create_index('idx_user_pinned_trees_user', 'user_pinned_trees', ['user_id'])
    op.create_index('idx_user_pinned_trees_tree', 'user_pinned_trees', ['tree_id'])


def downgrade() -> None:
    op.drop_index('idx_user_pinned_trees_tree', 'user_pinned_trees')
    op.drop_index('idx_user_pinned_trees_user', 'user_pinned_trees')
    op.drop_table('user_pinned_trees')
