-- Buat m_screening_template: master template screening klinik
CREATE TABLE IF NOT EXISTS one_klinik.m_screening_template (
  M_ScreeningTemplateID          INT(11)      NOT NULL AUTO_INCREMENT,
  M_ScreeningTemplateCode        VARCHAR(30)  NOT NULL DEFAULT '',
  M_ScreeningTemplateName        VARCHAR(100) NOT NULL DEFAULT '',
  M_ScreeningTemplateDescription VARCHAR(255) NULL     DEFAULT NULL,
  M_ScreeningTemplateIsActive    CHAR(1)      NOT NULL DEFAULT 'Y',
  M_ScreeningTemplateUserID      INT(11)      NULL     DEFAULT NULL,
  M_ScreeningTemplateCreated     DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP,
  M_ScreeningTemplateLastUpdated DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (M_ScreeningTemplateID),
  UNIQUE KEY uk_M_ScreeningTemplateCode (M_ScreeningTemplateCode),
  KEY idx_M_ScreeningTemplateIsActive (M_ScreeningTemplateIsActive)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Master template form screening klinik';

-- Buat m_screening_form: daftar pertanyaan per template
-- M_ScreeningFormOptions: JSON array pilihan jawaban, contoh: ["Ya","Tidak"]
-- M_ScreeningFormAnswerType: single=pilih satu, multi=pilih banyak, text=isian bebas
CREATE TABLE IF NOT EXISTS one_klinik.m_screening_form (
  M_ScreeningFormID                    INT(11)                       NOT NULL AUTO_INCREMENT,
  M_ScreeningFormM_ScreeningTemplateID INT(11)                       NOT NULL,
  M_ScreeningFormQuestion              TEXT                          NOT NULL,
  M_ScreeningFormAnswerType            ENUM('single','multi','text') NOT NULL DEFAULT 'single',
  M_ScreeningFormOptions               TEXT                          NULL DEFAULT NULL,
  M_ScreeningFormSortOrder             TINYINT(3) UNSIGNED           NOT NULL DEFAULT 0,
  M_ScreeningFormIsRequired            CHAR(1)                       NOT NULL DEFAULT 'Y',
  M_ScreeningFormIsActive              CHAR(1)                       NOT NULL DEFAULT 'Y',
  M_ScreeningFormUserID                INT(11)                       NULL DEFAULT NULL,
  M_ScreeningFormCreated               DATETIME                      NOT NULL DEFAULT CURRENT_TIMESTAMP,
  M_ScreeningFormLastUpdated           DATETIME                      NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (M_ScreeningFormID),
  KEY idx_M_ScreeningFormTemplateID (M_ScreeningFormM_ScreeningTemplateID),
  KEY idx_M_ScreeningFormIsActive (M_ScreeningFormIsActive),
  CONSTRAINT fk_sf_template
    FOREIGN KEY (M_ScreeningFormM_ScreeningTemplateID)
    REFERENCES m_screening_template (M_ScreeningTemplateID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Pertanyaan form screening per template';

-- ---------------------------------------------------------------------------
-- Data master template
-- ---------------------------------------------------------------------------
INSERT INTO one_klinik.m_screening_template
  (M_ScreeningTemplateCode, M_ScreeningTemplateName, M_ScreeningTemplateDescription, M_ScreeningTemplateIsActive)
VALUES
  ('VAKSINASI', 'Screening Vaksinasi',
   'Form screening pra-vaksinasi untuk menilai kelayakan pemberian vaksin', 'Y'),
  ('KHITAN', 'Screening Khitan',
   'Form screening pra-tindakan khitan untuk menilai kelayakan pasien', 'Y');

-- ---------------------------------------------------------------------------
-- Pertanyaan template VAKSINASI
-- ---------------------------------------------------------------------------
SET @vaksin_id = (
  SELECT M_ScreeningTemplateID
  FROM one_klinik.m_screening_template
  WHERE M_ScreeningTemplateCode = 'VAKSINASI'
);

INSERT INTO one_klinik.m_screening_form
  (M_ScreeningFormM_ScreeningTemplateID, M_ScreeningFormQuestion,
   M_ScreeningFormAnswerType, M_ScreeningFormOptions,
   M_ScreeningFormSortOrder, M_ScreeningFormIsRequired)
VALUES
  (@vaksin_id,
   'Apakah saat ini sedang demam (suhu ≥ 38°C)?',
   'single', '["Ya","Tidak"]', 1, 'Y'),

  (@vaksin_id,
   'Apakah sedang dalam kondisi sakit akut?',
   'single', '["Ya","Tidak"]', 2, 'Y'),

  (@vaksin_id,
   'Apakah memiliki riwayat anafilaksis atau reaksi alergi berat?',
   'single', '["Ya","Tidak","Tidak Tahu"]', 3, 'Y'),

  (@vaksin_id,
   'Apakah pernah mengalami reaksi alergi terhadap vaksin sebelumnya?',
   'single', '["Ya","Tidak","Belum Pernah Divaksin"]', 4, 'Y'),

  (@vaksin_id,
   'Apakah sedang mengonsumsi obat imunosupresan atau menjalani kemoterapi?',
   'single', '["Ya","Tidak"]', 5, 'Y'),

  (@vaksin_id,
   'Apakah sedang hamil atau menyusui?',
   'single', '["Ya","Tidak","Tidak Berlaku"]', 6, 'Y'),

  (@vaksin_id,
   'Apakah memiliki riwayat penyakit kronis yang relevan? (sebutkan jika ada)',
   'text', NULL, 7, 'N'),

  (@vaksin_id,
   'Kesimpulan: kelayakan pemberian vaksin',
   'single', '["Layak","Tidak Layak","Tunda"]', 8, 'Y'),

  (@vaksin_id,
   'Catatan petugas screening',
   'text', NULL, 9, 'N');

-- ---------------------------------------------------------------------------
-- Pertanyaan template KHITAN
-- ---------------------------------------------------------------------------
SET @khitan_id = (
  SELECT M_ScreeningTemplateID
  FROM one_klinik.m_screening_template
  WHERE M_ScreeningTemplateCode = 'KHITAN'
);

INSERT INTO one_klinik.m_screening_form
  (M_ScreeningFormM_ScreeningTemplateID, M_ScreeningFormQuestion,
   M_ScreeningFormAnswerType, M_ScreeningFormOptions,
   M_ScreeningFormSortOrder, M_ScreeningFormIsRequired)
VALUES
  (@khitan_id,
   'Apakah ada riwayat gangguan pembekuan darah atau hemofilia?',
   'single', '["Ya","Tidak","Tidak Tahu"]', 1, 'Y'),

  (@khitan_id,
   'Apakah ada riwayat alergi terhadap obat anestesi lokal (lidokain, prokain, dll)?',
   'single', '["Ya","Tidak","Tidak Tahu"]', 2, 'Y'),

  (@khitan_id,
   'Apakah menderita atau dicurigai Diabetes Mellitus?',
   'single', '["Ya","Tidak","Dicurigai"]', 3, 'Y'),

  (@khitan_id,
   'Apakah saat ini sedang sakit akut atau demam (suhu ≥ 38°C)?',
   'single', '["Ya","Tidak"]', 4, 'Y'),

  (@khitan_id,
   'Apakah sedang dalam pengobatan antikoagulan (pengencer darah)?',
   'single', '["Ya","Tidak"]', 5, 'Y'),

  (@khitan_id,
   'Apakah ada kelainan anatomi pada area genitalia?',
   'single', '["Ya","Tidak"]', 6, 'Y'),

  (@khitan_id,
   'Apakah pasien kooperatif untuk menjalani tindakan?',
   'single', '["Kooperatif","Cukup Kooperatif","Tidak Kooperatif"]', 7, 'Y'),

  (@khitan_id,
   'Berat badan pasien (kg)',
   'text', NULL, 8, 'Y'),

  (@khitan_id,
   'Tinggi badan pasien (cm)',
   'text', NULL, 9, 'Y'),

  (@khitan_id,
   'Apakah ada riwayat tindakan bedah sebelumnya pada area genitalia?',
   'single', '["Ya","Tidak"]', 10, 'N'),

  (@khitan_id,
   'Kesimpulan: kelayakan tindakan khitan',
   'single', '["Layak","Tidak Layak","Tunda"]', 11, 'Y'),

  (@khitan_id,
   'Catatan petugas screening',
   'text', NULL, 12, 'N');
