"""add personnel_number to users + company config

Revision ID: 0020
Revises: 0019
Create Date: 2026-05-05
"""
from alembic import op
import sqlalchemy as sa


revision = "0020"
down_revision = "0019"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # users.personnel_number with format constraint
    op.add_column(
        "users",
        sa.Column("personnel_number", sa.String(length=50), nullable=True),
    )
    op.create_check_constraint(
        "ck_users_personnel_number_digits",
        "users",
        "personnel_number IS NULL OR personnel_number ~ '^[0-9]+$'",
    )
    # Partial unique index per company (NULLs allowed, reserved on deactivation)
    op.create_index(
        "ix_users_company_personnel_number",
        "users",
        ["company_id", "personnel_number"],
        unique=True,
        postgresql_where=sa.text("personnel_number IS NOT NULL"),
    )

    # companies config
    op.add_column(
        "companies",
        sa.Column(
            "personnel_number_required",
            sa.Boolean(),
            nullable=False,
            server_default="false",
        ),
    )
    op.add_column(
        "companies",
        sa.Column(
            "personnel_number_mode",
            sa.String(length=10),
            nullable=False,
            server_default="manual",
        ),
    )
    op.add_column(
        "companies",
        sa.Column(
            "personnel_number_next",
            sa.Integer(),
            nullable=False,
            server_default="1",
        ),
    )

    # ldap_configs.attr_personnel_number (optional mapping)
    op.add_column(
        "ldap_configs",
        sa.Column("attr_personnel_number", sa.String(length=100), nullable=True),
    )


def downgrade() -> None:
    op.drop_column("ldap_configs", "attr_personnel_number")
    op.drop_column("companies", "personnel_number_next")
    op.drop_column("companies", "personnel_number_mode")
    op.drop_column("companies", "personnel_number_required")
    op.drop_index("ix_users_company_personnel_number", table_name="users")
    op.drop_constraint("ck_users_personnel_number_digits", "users", type_="check")
    op.drop_column("users", "personnel_number")
