-- IWRS tabulky pro databázi studie
-- Spustit jednou: mysql -h 192.168.1.76 -u root -p studie < create_iwrs_tables.sql

USE studie;

-- ── Import log ───────────────────────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS iwrs_import (
    import_id   INT AUTO_INCREMENT PRIMARY KEY,
    study       VARCHAR(20)  NOT NULL,
    imported_at DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP,
    source_file VARCHAR(500) NOT NULL,
    INDEX idx_study (study)
);

-- ── UCO3001 subject summary ───────────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS iwrs_uco3001_subject_summary (
    id                                  INT AUTO_INCREMENT PRIMARY KEY,
    import_id                           INT          NOT NULL,
    subject                             VARCHAR(20)  NOT NULL,
    prior_subject_identifier            VARCHAR(20),
    site                                VARCHAR(50),
    investigator                        VARCHAR(100),
    location                            VARCHAR(50),
    cohort_per_irt                      VARCHAR(100),
    informed_consent_date               DATE,
    adolescent_assent_date              DATE,
    age                                 SMALLINT,
    weight                              DECIMAL(5,1),
    rescreened_subject                  VARCHAR(10),
    adt_ir                              VARCHAR(10),
    three_or_more_advanced_therapies    VARCHAR(10),
    only_oral_5asa_compounds            VARCHAR(10),
    ustekinumab                         VARCHAR(10),
    isolated_proctitis                  VARCHAR(10),
    clinical_responder_status_i12_m0    VARCHAR(100),
    irt_subject_status                  VARCHAR(50),
    i0_rand_date_local                  DATE,
    last_irt_transaction                VARCHAR(100),
    last_irt_transaction_date_local     DATE,
    last_irt_transaction_date_utc       DATE,
    next_irt_transaction                VARCHAR(100),
    next_irt_transaction_date_local     DATE,
    most_recent_med_assignment_date     DATE,
    days_since_last_med_assignment      SMALLINT,
    patient_forecast_status             VARCHAR(50),
    patient_forecast_status_changed_date DATE,
    FOREIGN KEY (import_id) REFERENCES iwrs_import(import_id),
    INDEX idx_import (import_id),
    INDEX idx_subject (subject)
);

-- ── MDD3003 subject summary ───────────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS iwrs_mdd3003_subject_summary (
    id                                      INT AUTO_INCREMENT PRIMARY KEY,
    import_id                               INT          NOT NULL,
    subject                                 VARCHAR(20)  NOT NULL,
    prior_subject_identifier                VARCHAR(20),
    site                                    VARCHAR(50),
    investigator                            VARCHAR(100),
    location                                VARCHAR(50),
    cohort_per_irt                          VARCHAR(50),
    madrs_criteria_integrated               VARCHAR(50),
    informed_consent_date                   DATE,
    age                                     SMALLINT,
    madrs_criteria_v15                      VARCHAR(10),
    madrs_criteria_v16                      VARCHAR(10),
    madrs_criteria_v17                      VARCHAR(10),
    stratification_country                  VARCHAR(10),
    age_group                               VARCHAR(20),
    stable_remitters                        VARCHAR(50),
    irt_subject_status                      VARCHAR(100),
    last_irt_transaction                    VARCHAR(100),
    last_irt_transaction_date_local         DATE,
    last_irt_transaction_date_utc           DATE,
    next_irt_transaction                    VARCHAR(100),
    next_irt_transaction_date_local         DATE,
    date_screened                           DATE,
    date_screen_failed                      DATE,
    date_randomized_part1                   DATE,
    date_early_withdraw_randomized_part1    DATE,
    date_open_label_induction               DATE,
    date_early_withdraw_open_label_induction DATE,
    date_randomized_part2                   DATE,
    date_early_withdraw_randomized_part2    DATE,
    date_completed                          DATE,
    date_unblinded                          DATE,
    FOREIGN KEY (import_id) REFERENCES iwrs_import(import_id),
    INDEX idx_import (import_id),
    INDEX idx_subject (subject)
);

-- ── Subject visits / transactions (obě studie) ───────────────────────────────
CREATE TABLE IF NOT EXISTS iwrs_subject_visits (
    id                          INT AUTO_INCREMENT PRIMARY KEY,
    import_id                   INT          NOT NULL,
    study                       VARCHAR(20)  NOT NULL,
    subject                     VARCHAR(20)  NOT NULL,
    visit_type                  ENUM('Past','Upcoming') NOT NULL,
    scheduled_date              DATE,
    window_days                 VARCHAR(20),
    actual_date                 DATE,
    irt_transaction_no          SMALLINT,
    irt_transaction_description VARCHAR(200),
    medication_assignment       VARCHAR(200),
    quantity_assigned           SMALLINT,
    medication_id               VARCHAR(20),
    FOREIGN KEY (import_id) REFERENCES iwrs_import(import_id),
    INDEX idx_import (import_id),
    INDEX idx_study_subject (study, subject)
);
