-- Research Commons M4: study information and participation requests.
-- Select the Research Commons database before running this file.
-- The opportunities table must use InnoDB and have an indexed integer opp_id.
-- Each DDL statement commits independently. Reruns skip existing columns.
-- The app reads these public metadata fields from opportunities directly.
-- No changes to view_opportunities or existing study data are needed.
-- study_mode values: online, in_person, hybrid. NULL means not provided.

SET @study_ddl = IF(
    EXISTS (SELECT 1 FROM information_schema.COLUMNS
            WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'opportunities'
              AND COLUMN_NAME = 'posted_at'),
    'SELECT 1',
    'ALTER TABLE opportunities ADD COLUMN posted_at DATETIME NULL DEFAULT NULL'
);
PREPARE study_statement FROM @study_ddl;
EXECUTE study_statement;
DEALLOCATE PREPARE study_statement;

-- Retain existing dates and any existing publication default. When the
-- nullable DATETIME column has no default, date subsequent inserts.
SET @study_ddl = IF(
    EXISTS (SELECT 1 FROM information_schema.COLUMNS
            WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'opportunities'
              AND COLUMN_NAME = 'posted_at' AND DATA_TYPE = 'datetime'
              AND IS_NULLABLE = 'YES' AND EXTRA NOT LIKE '%GENERATED%'
              AND (COLUMN_DEFAULT IS NULL OR UPPER(COLUMN_DEFAULT) = 'NULL')),
    'ALTER TABLE opportunities MODIFY COLUMN posted_at DATETIME NULL DEFAULT CURRENT_TIMESTAMP',
    'SELECT 1'
);
PREPARE study_statement FROM @study_ddl;
EXECUTE study_statement;
DEALLOCATE PREPARE study_statement;

SET @study_ddl = IF(
    EXISTS (SELECT 1 FROM information_schema.COLUMNS
            WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'opportunities'
              AND COLUMN_NAME = 'study_mode'),
    'SELECT 1',
    'ALTER TABLE opportunities ADD COLUMN study_mode VARCHAR(20) NULL'
);
PREPARE study_statement FROM @study_ddl;
EXECUTE study_statement;
DEALLOCATE PREPARE study_statement;

SET @study_ddl = IF(
    EXISTS (SELECT 1 FROM information_schema.COLUMNS
            WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'opportunities'
              AND COLUMN_NAME = 'study_location'),
    'SELECT 1',
    'ALTER TABLE opportunities ADD COLUMN study_location VARCHAR(255) NULL'
);
PREPARE study_statement FROM @study_ddl;
EXECUTE study_statement;
DEALLOCATE PREPARE study_statement;

SET @study_ddl = IF(
    EXISTS (SELECT 1 FROM information_schema.COLUMNS
            WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'opportunities'
              AND COLUMN_NAME = 'study_schedule'),
    'SELECT 1',
    'ALTER TABLE opportunities ADD COLUMN study_schedule VARCHAR(255) NULL'
);
PREPARE study_statement FROM @study_ddl;
EXECUTE study_statement;
DEALLOCATE PREPARE study_statement;

SET @study_ddl = IF(
    EXISTS (SELECT 1 FROM information_schema.COLUMNS
            WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'opportunities'
              AND COLUMN_NAME = 'researcher_affiliation'),
    'SELECT 1',
    'ALTER TABLE opportunities ADD COLUMN researcher_affiliation VARCHAR(255) NULL'
);
PREPARE study_statement FROM @study_ddl;
EXECUTE study_statement;
DEALLOCATE PREPARE study_statement;

SET @study_ddl = IF(
    EXISTS (SELECT 1 FROM information_schema.COLUMNS
            WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'opportunities'
              AND COLUMN_NAME = 'incentive_details'),
    'SELECT 1',
    'ALTER TABLE opportunities ADD COLUMN incentive_details VARCHAR(255) NULL'
);
PREPARE study_statement FROM @study_ddl;
EXECUTE study_statement;
DEALLOCATE PREPARE study_statement;

SET @study_ddl = IF(
    EXISTS (SELECT 1 FROM information_schema.COLUMNS
            WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'opportunities'
              AND COLUMN_NAME = 'data_use'),
    'SELECT 1',
    'ALTER TABLE opportunities ADD COLUMN data_use TEXT NULL'
);
PREPARE study_statement FROM @study_ddl;
EXECUTE study_statement;
DEALLOCATE PREPARE study_statement;

-- The unique key treats different casing of the same address as one request.
-- Deleting an opportunity also removes its participation requests so the
-- existing admin delete operation can succeed without leaving contact data behind.
-- Match the base primary key's integer storage type and signedness. This
-- keeps the foreign key valid if the team uses a signed INT for opp_id.
SET @study_opp_id_type = (
    SELECT CONCAT(UPPER(DATA_TYPE), IF(LOCATE('unsigned', COLUMN_TYPE) > 0, ' UNSIGNED', ''))
    FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'opportunities'
      AND COLUMN_NAME = 'opp_id'
      AND DATA_TYPE IN ('tinyint', 'smallint', 'mediumint', 'int', 'bigint')
);
SET @study_ddl = CONCAT(
    'CREATE TABLE IF NOT EXISTS study_signups (
    signup_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    opp_id ', @study_opp_id_type, ' NOT NULL,
    participant_name VARCHAR(120) NOT NULL,
    participant_email VARCHAR(254) CHARACTER SET ascii COLLATE ascii_general_ci NOT NULL,
    academic_year VARCHAR(40) NOT NULL,
    participant_note VARCHAR(1000) NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (signup_id),
    UNIQUE KEY study_signup_email (opp_id, participant_email),
    CONSTRAINT study_signups_opportunity
        FOREIGN KEY (opp_id) REFERENCES opportunities (opp_id) ON DELETE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4'
);
PREPARE study_statement FROM @study_ddl;
EXECUTE study_statement;
DEALLOCATE PREPARE study_statement;

SET @study_ddl = NULL;
SET @study_opp_id_type = NULL;
