"""add month snapshot tables

Revision ID: 03ebe3c07262
Revises: f1ccdc4bc1bf
Create Date: 2026-04-17 11:33:45.853510

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


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


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('month',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('year_month', sa.String(length=7), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('year_month')
    )
    op.create_table('month_entry',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('month_id', sa.Integer(), nullable=False),
    sa.Column('section', sa.Enum('income', 'fixed_bill', 'debt_minimum', 'food', 'subscription', 'other', name='section', native_enum=False, length=32), nullable=False),
    sa.Column('name', sa.String(length=128), nullable=False),
    sa.Column('planned', sa.Numeric(precision=10, scale=2), nullable=False),
    sa.Column('applied', sa.Numeric(precision=10, scale=2), server_default='0.00', nullable=False),
    sa.Column('origin_name', sa.String(length=128), nullable=True),
    sa.Column('origin_planned', sa.Numeric(precision=10, scale=2), nullable=True),
    sa.Column('source_entry_id', sa.Integer(), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.ForeignKeyConstraint(['month_id'], ['month.id'], ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['source_entry_id'], ['entry.id'], ondelete='SET NULL'),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('month_id', 'id')
    )
    with op.batch_alter_table('month_entry', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_month_entry_month_id'), ['month_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_month_entry_section'), ['section'], unique=False)

    op.create_table('month_debt_target',
    sa.Column('month_id', sa.Integer(), autoincrement=False, nullable=False),
    sa.Column('month_entry_id', sa.Integer(), nullable=True),
    sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.ForeignKeyConstraint(['month_entry_id'], ['month_entry.id'], ondelete='SET NULL'),
    sa.ForeignKeyConstraint(['month_id'], ['month.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('month_id')
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('month_debt_target')
    with op.batch_alter_table('month_entry', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_month_entry_section'))
        batch_op.drop_index(batch_op.f('ix_month_entry_month_id'))

    op.drop_table('month_entry')
    op.drop_table('month')
    # ### end Alembic commands ###
