"""Add folder_type and emails_count fields to Folder table

Revision ID: 7b6db971e3a4
Revises: 29a9c4ac0fc6
Create Date: 2025-08-06 12:25:13.415140

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '7b6db971e3a4'
down_revision = '29a9c4ac0fc6'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    # Add new columns as nullable first
    op.add_column('folders', sa.Column('folder_type', sa.String(length=20), nullable=True))
    op.add_column('folders', sa.Column('emails_count', sa.Integer(), nullable=True))
    
    # Set default values for existing records
    # Set all existing folders to 'destination' type
    op.execute("UPDATE folders SET folder_type = 'destination' WHERE folder_type IS NULL")
    
    # Set 'inbox' folders to 'tidy' type
    op.execute("UPDATE folders SET folder_type = 'tidy' WHERE folder_type IS NULL AND name ILIKE '%inbox%'")
    
    # Now make folder_type NOT NULL
    op.alter_column('folders', 'folder_type', nullable=False)
    
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    # Make column nullable again before dropping
    op.alter_column('folders', 'folder_type', nullable=True)
    
    # Drop the columns
    op.drop_column('folders', 'emails_count')
    op.drop_column('folders', 'folder_type')
    
    # ### end Alembic commands ###
