-- 1/3 : Création de la table temporaire
DROP TABLE IF EXISTS w_dbsetup
;

CREATE TEMP TABLE w_dbsetup AS
SELECT
    t_etude_cardio_mentions.code,
    t_etude_cardio_mentions.texte,
    t_etude_cardio_mentions.texte_court,
    t_etude_cardio_mentions.seuil_activite_1,
    t_etude_cardio_mentions.seuil_activite_2,
    t_etude_cardio_modalites.code AS cardio_modalite_code,
    t_etude_cardio_mentions.type AS type,
    t_etude_cardio_mentions.cdc
FROM pmsi.t_etude_cardio_mentions
         JOIN pmsi.t_etude_cardio_modalites ON t_etude_cardio_modalites.oid = cardio_modalite_id
LIMIT 0
;

-- 2/3 : Peuplement de la table temporaire
INSERT INTO w_dbsetup(
    code,
    texte,
    texte_court,
    seuil_activite_1,
    seuil_activite_2,
    cardio_modalite_code,
    type,
    cdc
)
VALUES
    ('CONGA', 'Cardiopathies congenitales hors rythmologie - Mention A', 'Mention A', 40, 0, 'CONG', 'A', 2022),
    ('CONGB', 'Cardiopathies congenitales hors rythmologie - Mention B', 'Mention B', 80, 0, 'CONG', 'B', 2022),
    ('ISCHA', 'Cardiopathies ischémiques et structurelles de l?adulte - Mention A', 'Mention A', 400, 15, 'ISCH', 'A', 2022),
    ('RYTHA', 'Rythmologie interventionnelle - Mention A', 'Mention A', 50, 0, 'RYTH', 'A', 2022),
    ('RYTHB', 'Rythmologie interventionnelle - Mention B', 'Mention B', 50, 50, 'RYTH', 'B', 2022),
    ('RYTHC', 'Rythmologie interventionnelle - Mention C', 'Mention C', 100, 0, 'RYTH', 'C', 2022),
    ('RYTHD', 'Rythmologie interventionnelle - Mention D', 'Mention D', 100, 0, 'RYTH', 'D', 2022)
;

-- 3/3 : Màj de la table iCTI
UPDATE pmsi.t_etude_cardio_mentions SET
                                        texte = w_dbsetup.texte,
                                        texte_court = w_dbsetup.texte_court,
                                        seuil_activite_1 = w_dbsetup.seuil_activite_1,
                                        seuil_activite_2 = w_dbsetup.seuil_activite_2,
                                        cardio_modalite_id = t_etude_cardio_modalites.oid,
                                        type = w_dbsetup.type
FROM w_dbsetup
         JOIN pmsi.t_etude_cardio_modalites ON t_etude_cardio_modalites.code = cardio_modalite_code
WHERE 1=1
  AND t_etude_cardio_mentions.code = w_dbsetup.code
  AND t_etude_cardio_mentions.cdc = w_dbsetup.cdc
  AND (
    t_etude_cardio_mentions.texte IS DISTINCT FROM w_dbsetup.texte
        OR t_etude_cardio_mentions.texte_court IS DISTINCT FROM w_dbsetup.texte_court
        OR t_etude_cardio_mentions.seuil_activite_1 IS DISTINCT FROM w_dbsetup.seuil_activite_1
        OR t_etude_cardio_mentions.seuil_activite_2 IS DISTINCT FROM w_dbsetup.seuil_activite_2
        OR t_etude_cardio_mentions.cardio_modalite_id IS DISTINCT FROM t_etude_cardio_modalites.oid
        OR t_etude_cardio_mentions.type IS DISTINCT FROM w_dbsetup.type
    )
;

INSERT INTO pmsi.t_etude_cardio_mentions(
    code,
    texte,
    texte_court,
    seuil_activite_1,
    seuil_activite_2,
    cardio_modalite_id,
    type,
    cdc
)
SELECT
    w_dbsetup.code,
    w_dbsetup.texte,
    w_dbsetup.texte_court,
    w_dbsetup.seuil_activite_1,
    w_dbsetup.seuil_activite_2,
    t_etude_cardio_modalites.oid,
    w_dbsetup.type,
    w_dbsetup.cdc
FROM w_dbsetup
         JOIN pmsi.t_etude_cardio_modalites ON t_etude_cardio_modalites.code = w_dbsetup.cardio_modalite_code
         LEFT JOIN pmsi.t_etude_cardio_mentions ON t_etude_cardio_mentions.code = w_dbsetup.code
    AND t_etude_cardio_mentions.cdc = w_dbsetup.cdc
WHERE t_etude_cardio_mentions.code IS NULL OR t_etude_cardio_mentions.cdc IS NULL
;