CREATE TABLE IF NOT EXISTS qr_sessions (
    id           BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    token        CHAR(64)        NOT NULL,
    user_id      BIGINT UNSIGNED DEFAULT NULL,
    status       ENUM('pending','scanned','confirmed','expired') DEFAULT 'pending',
    ip_address   VARCHAR(45)     DEFAULT NULL,
    expires_at   DATETIME        NOT NULL,
    scanned_at   DATETIME        DEFAULT NULL,
    confirmed_at DATETIME        DEFAULT NULL,
    created_at   DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY uq_token (token),
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL,
    INDEX idx_status (status),
    INDEX idx_expires (expires_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
