"""add tree status field for draft/published workflow

Revision ID: 025
Revises: 25b001abd0f7
Create Date: 2026-02-08

Adds status field to trees table for draft/published workflow:
- status: enum ('draft', 'published') - defaults to 'published' for existing trees
- Drafts allow incomplete/invalid structures for work-in-progress
- Published trees require validation before saving
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


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


def upgrade() -> None:
    # Add status field with default 'published' (for backward compatibility)
    op.add_column('trees', sa.Column(
        'status',
        sa.String(20),
        nullable=False,
        server_default='published',
        comment="Status: draft (work in progress) or published (validated and available)"
    ))
    op.create_index('ix_trees_status', 'trees', ['status'])

    # Add CHECK constraint for status values
    op.create_check_constraint(
        'ck_trees_status',
        'trees',
        "status IN ('draft', 'published')"
    )


def downgrade() -> None:
    op.drop_index('ix_trees_status', table_name='trees')
    op.drop_constraint('ck_trees_status', 'trees', type_='check')
    op.drop_column('trees', 'status')
