CREATE TABLE IF NOT EXISTS attendance_kiosks (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    kiosk_name VARCHAR(150) NOT NULL,
    event_id BIGINT UNSIGNED NOT NULL,
    session_id BIGINT UNSIGNED NOT NULL,
    scan_type ENUM('time_in','time_out') NOT NULL DEFAULT 'time_in',
    access_token VARCHAR(64) NOT NULL,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    created_by BIGINT UNSIGNED NULL,
    last_accessed_at DATETIME NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uq_attendance_kiosks_token (access_token),
    KEY idx_attendance_kiosks_event_id (event_id),
    KEY idx_attendance_kiosks_session_id (session_id),
    KEY idx_attendance_kiosks_active (is_active),
    CONSTRAINT fk_attendance_kiosks_event
        FOREIGN KEY (event_id) REFERENCES events(id)
        ON UPDATE CASCADE
        ON DELETE CASCADE,
    CONSTRAINT fk_attendance_kiosks_session
        FOREIGN KEY (session_id) REFERENCES event_sessions(id)
        ON UPDATE CASCADE
        ON DELETE CASCADE,
    CONSTRAINT fk_attendance_kiosks_created_by
        FOREIGN KEY (created_by) REFERENCES users(id)
        ON UPDATE CASCADE
        ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
