"""Add kiosk_nfc_uid to users table

Revision ID: 0025
Revises: 0024
Create Date: 2026-05-24
"""
from alembic import op
import sqlalchemy as sa

revision = "0025"
down_revision = "0024"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # NFC-UID für Kiosk-Login (optional, eindeutig pro Firma über partial unique index)
    op.add_column("users", sa.Column("kiosk_nfc_uid", sa.String(64), nullable=True))
    op.create_index(
        "ix_users_kiosk_nfc_uid",
        "users",
        ["kiosk_nfc_uid"],
        unique=False,  # Uniqueness nur innerhalb company → partial index
    )
    # Partial unique index: NFC-UID eindeutig innerhalb einer Firma
    op.execute("""
        CREATE UNIQUE INDEX IF NOT EXISTS uq_users_nfc_uid_per_company
        ON users (company_id, kiosk_nfc_uid)
        WHERE kiosk_nfc_uid IS NOT NULL
    """)


def downgrade() -> None:
    op.execute("DROP INDEX IF EXISTS uq_users_nfc_uid_per_company")
    op.drop_index("ix_users_kiosk_nfc_uid", table_name="users")
    op.drop_column("users", "kiosk_nfc_uid")
