"""Per-type auto-expiry rule on notificationtypes

Makes the shopfloor auto-expiry window configurable per notification type
instead of hardcoding recognition (8 AM Eastern) and recertification (14 days)
in the API. Adds:
  expirymode   'none' | 'duration' | 'dailytime'
  expirydays   days for 'duration'
  expiryhour   hour (Eastern) for 'dailytime'
  expiryminute minute (Eastern) for 'dailytime'

Seeds the two existing rule-bearing types so behavior is unchanged:
  Recognition (typecolor 'recognition')     -> dailytime 08:00 Eastern
  Recertification (typecolor 'recertification') -> duration 14 days

Revision ID: 7d03_notificationtype_expiry
Revises: 7d02_widen_notification_employee_cols
Create Date: 2026-07-08

"""
from alembic import op
import sqlalchemy as sa


revision = '7d03_notificationtype_expiry'
down_revision = '7d02_widen_notification_employee_cols'
branch_labels = None
depends_on = None


def upgrade():
    op.add_column('notificationtypes', sa.Column('expirymode', sa.String(length=20), nullable=True, server_default='none'))
    op.add_column('notificationtypes', sa.Column('expirydays', sa.Integer(), nullable=True))
    op.add_column('notificationtypes', sa.Column('expiryhour', sa.SmallInteger(), nullable=True))
    op.add_column('notificationtypes', sa.Column('expiryminute', sa.SmallInteger(), nullable=True, server_default='0'))

    # preserve current behavior for the two rule-bearing types
    op.execute(
        "UPDATE notificationtypes SET expirymode='dailytime', expiryhour=8, expiryminute=0 "
        "WHERE typecolor='recognition'"
    )
    op.execute(
        "UPDATE notificationtypes SET expirymode='duration', expirydays=14 "
        "WHERE typecolor='recertification'"
    )
    # everything else: explicit 'none' (indefinite unless the creator sets an end time)
    op.execute("UPDATE notificationtypes SET expirymode='none' WHERE expirymode IS NULL")


def downgrade():
    op.drop_column('notificationtypes', 'expiryminute')
    op.drop_column('notificationtypes', 'expiryhour')
    op.drop_column('notificationtypes', 'expirydays')
    op.drop_column('notificationtypes', 'expirymode')
