"""Two-stage absence approval (agent-12)

Revision ID: 0038
Revises: 0037
Create Date: 2026-06-23

- AbsenceStatus.FIRST_APPROVED (erste Stufe erteilt, wartet auf finale Genehmigung)
- absences.first_approved_by (Genehmiger Stufe 1; muss != finalem Genehmiger sein)
- companies.two_stage_approval_enabled + two_stage_min_days (firmenweites Opt-in)
"""
from alembic import op

revision = "0038"
down_revision = "0037"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.execute("ALTER TYPE absencestatus ADD VALUE IF NOT EXISTS 'first_approved'")
    op.execute(
        "ALTER TABLE absences ADD COLUMN IF NOT EXISTS first_approved_by UUID "
        "REFERENCES users(id) ON DELETE SET NULL"
    )
    op.execute(
        "ALTER TABLE companies ADD COLUMN IF NOT EXISTS "
        "two_stage_approval_enabled BOOLEAN NOT NULL DEFAULT FALSE"
    )
    op.execute(
        "ALTER TABLE companies ADD COLUMN IF NOT EXISTS "
        "two_stage_min_days INTEGER NOT NULL DEFAULT 0"
    )


def downgrade() -> None:
    # Enum-Wert bleibt (PostgreSQL kann ihn nicht entfernen).
    op.execute("ALTER TABLE companies DROP COLUMN IF EXISTS two_stage_min_days")
    op.execute("ALTER TABLE companies DROP COLUMN IF EXISTS two_stage_approval_enabled")
    op.execute("ALTER TABLE absences DROP COLUMN IF EXISTS first_approved_by")
