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

CREATE TEMP TABLE w_dbsetup AS
    SELECT 
    	CASE WHEN t_classes.oid = 0 THEN t_classes.oid ELSE 1 END AS oid,
    	t_listes_tables.code as table_code,
    	t_classes.code,
    	t_classes.sequence AS sequence_source, 
    	t_classes.texte,
    	t_classes.is_cti,
        t_classes.contenu_by_code,
        t_classes.contenu_non_replicable_ailleurs,
    	0::numeric AS sequence,
    	0::bigint AS table_id
    FROM compta.t_classes
    JOIN compta.t_listes_tables ON table_id = t_listes_tables.oid 
    WHERE t_classes.code <> '' AND
    	(is_cti = '1'  OR t_classes.code ilike 'CTI_%') AND
    	1=1
    ORDER BY 2,3 LIMIT 0
;

-- 2/3 : Peuplement de la table temporaire
INSERT INTO w_dbsetup(
    oid,
    table_code,
    code,
    sequence_source,
    texte,
    is_cti,
    contenu_by_code,
    contenu_non_replicable_ailleurs,
    sequence,
    table_id
)
VALUES
(1, 'CPT', 'CTI_CONTRIBUTION_BLOC_OPERATOIRE', 42, 'Coût des charges pour le calcul de la contribution', '1', '0', '0', 0, 0)
;

-- 3/3 : Màj de la table iCTI
UPDATE w_dbsetup
SET table_id = t_listes_tables.oid
FROM compta.t_listes_tables
WHERE t_listes_tables.Code = table_code
;

UPDATE w_dbsetup
SET sequence = t_classes.sequence
FROM compta.t_classes
WHERE t_classes.code = w_dbsetup.code AND
	t_classes.table_id = w_dbsetup.table_id
;

UPDATE w_dbsetup
SET sequence = sequence_source
WHERE code NOT IN (SELECT code FROM compta.t_classes) AND
	w_dbsetup.table_id::text || '-' || w_dbsetup.sequence_source::text NOT IN (SELECT table_id::text || '-' || sequence::text FROM compta.t_classes)
;

UPDATE w_dbsetup
SET sequence = subview.sequence
FROM
(
SELECT w_dbsetup.table_id, sequence_source,
CASE WHEN sequence1 < 7 THEN sequence1 WHEN sequence2 <= 27 THEN sequence2 ELSE sequence3 END + row_number() OVER (PARTITION BY w_dbsetup.table_id ORDER BY code) AS sequence 
FROM w_dbsetup
JOIN 
	(
	SELECT t_listes_tables.oid AS table_id,
		MAX(CASE WHEN sequence < 7 THEN sequence ELSE 0 END) AS sequence1,
		MAX(CASE WHEN sequence BETWEEN 10 AND 27 THEN sequence ELSE 9 END) AS sequence2,
		MAX(CASE WHEN sequence > 30 THEN sequence ELSE 30 END) AS sequence3
	FROM compta.t_listes_tables
	LEFT JOIN compta.t_classes ON t_listes_tables.oid = table_id
	GROUP BY 1
	) subview ON w_dbsetup.table_id = subview.table_id
WHERE sequence = 0
) subview
WHERE w_dbsetup.table_id = subview.table_id AND
	w_dbsetup.sequence_source = subview.sequence_source AND
	w_dbsetup.sequence = 0
;

UPDATE compta.t_classes SET
	texte = w_dbsetup.texte,       
	table_id = w_dbsetup.table_id,      
	is_cti = w_dbsetup.is_cti,
    contenu_by_code = w_dbsetup.contenu_by_code,
    contenu_non_replicable_ailleurs = w_dbsetup.contenu_non_replicable_ailleurs
FROM w_dbsetup
WHERE 1=1
	AND t_classes.code = w_dbsetup.code
	AND t_classes.table_id = w_dbsetup.table_id
    AND (1!=1
        OR t_classes.texte IS DISTINCT FROM w_dbsetup.texte
        OR t_classes.is_cti IS DISTINCT FROM w_dbsetup.is_cti
        OR t_classes.contenu_by_code IS DISTINCT FROM w_dbsetup.contenu_by_code
        OR t_classes.contenu_non_replicable_ailleurs IS DISTINCT FROM w_dbsetup.contenu_non_replicable_ailleurs)
;

INSERT INTO compta.t_classes(
	code, 
	texte, 
	table_id, 
	is_cti, 
	sequence, 
	contenu_by_code, 
	contenu_non_replicable_ailleurs
	)
SELECT 
	w_dbsetup.code, 
	w_dbsetup.texte, 
	w_dbsetup.table_id, 
	w_dbsetup.is_cti, 
	w_dbsetup.sequence,
	w_dbsetup.contenu_by_code,
	w_dbsetup.contenu_non_replicable_ailleurs
FROM w_dbsetup
WHERE 1=1
	AND w_dbsetup.sequence <> 0
	AND w_dbsetup.table_id::text || '-' || w_dbsetup.sequence::text NOT IN (SELECT table_id::text || '-' || sequence::text FROM compta.t_classes)
	AND w_dbsetup.texte NOT LIKE '*SUPPRIME%'
;