CREATE TABLE IF NOT EXISTS work_session_events (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    work_session_id BIGINT UNSIGNED NOT NULL,
    user_id BIGINT UNSIGNED NOT NULL,
    event_type VARCHAR(50) NOT NULL,
    event_time DATETIME NOT NULL,
    latitude DECIMAL(10,8) DEFAULT NULL,
    longitude DECIMAL(11,8) DEFAULT NULL,
    accuracy_meters DECIMAL(8,2) DEFAULT NULL,
    device_info VARCHAR(255) DEFAULT NULL,
    notes VARCHAR(255) DEFAULT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    KEY idx_wse_session (work_session_id),
    KEY idx_wse_user_time (user_id, event_time),
    CONSTRAINT fk_wse_session
        FOREIGN KEY (work_session_id) REFERENCES work_sessions(id)
        ON UPDATE CASCADE
        ON DELETE CASCADE,
    CONSTRAINT fk_wse_user
        FOREIGN KEY (user_id) REFERENCES users(id)
        ON UPDATE CASCADE
        ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
