-- ============================================================================
-- PROTOTYPE TESTING MIGRATION
-- Complete database setup for prototype testing functionality
-- Run this SQL in phpMyAdmin
-- ============================================================================

-- Step 1: Add prototype_url column to studies table
-- This stores the URL of the prototype to be tested
ALTER TABLE `studies` 
ADD COLUMN IF NOT EXISTS `prototype_url` TEXT NULL AFTER `description`;

-- Step 2: Ensure tasks table exists with proper structure
-- Tasks define specific activities for participants during prototype testing
CREATE TABLE IF NOT EXISTS `tasks` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `study_id` INT(11) NOT NULL,
  `title` VARCHAR(255) NOT NULL,
  `description` TEXT NULL,
  `sequence` INT(11) NOT NULL DEFAULT 0,
  `success_rule_type` VARCHAR(50) NULL COMMENT 'selector, url, or javascript',
  `success_rule_value` TEXT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NULL ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_study_id` (`study_id`),
  KEY `idx_sequence` (`sequence`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Step 3: Verify the changes
DESCRIBE `studies`;
DESCRIBE `tasks`;

-- ============================================================================
-- NOTES:
-- ============================================================================
-- 1. prototype_url: URL of the prototype (e.g., https://example.com/prototype)
-- 2. tasks.success_rule_type can be:
--    - 'selector': Task succeeds when element is clicked
--    - 'url': Task succeeds when specific URL is reached
--    - 'javascript': Custom JS condition for success
-- 3. All events (clicks, scrolls, pins) are tracked in the existing 'events' table
-- ============================================================================

