"""add maintenance_schedules table

Revision ID: 0fd2a90a9c2c
Revises: 6e8128ef2aa8
Create Date: 2026-02-17

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

# revision identifiers, used by Alembic.
revision = '0fd2a90a9c2c'
down_revision = '6e8128ef2aa8'
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        'maintenance_schedules',
        sa.Column('id', postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column('tree_id', postgresql.UUID(as_uuid=True),
                  sa.ForeignKey('trees.id', ondelete='CASCADE'), nullable=False),
        sa.Column('created_by', postgresql.UUID(as_uuid=True),
                  sa.ForeignKey('users.id', ondelete='SET NULL'), nullable=True),
        sa.Column('cron_expression', sa.String(100), nullable=False),
        sa.Column('timezone', sa.String(100), nullable=False, server_default='UTC'),
        sa.Column('target_list_id', postgresql.UUID(as_uuid=True),
                  sa.ForeignKey('target_lists.id', ondelete='SET NULL'), nullable=True),
        sa.Column('is_active', sa.Boolean(), nullable=False, server_default='true'),
        sa.Column('next_run_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('last_run_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
        sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False),
    )
    op.create_index('ix_maintenance_schedules_tree_id', 'maintenance_schedules', ['tree_id'])
    op.create_unique_constraint('uq_maintenance_schedules_tree_id', 'maintenance_schedules', ['tree_id'])


def downgrade() -> None:
    op.drop_constraint('uq_maintenance_schedules_tree_id', 'maintenance_schedules', type_='unique')
    op.drop_index('ix_maintenance_schedules_tree_id', table_name='maintenance_schedules')
    op.drop_table('maintenance_schedules')
