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

CREATE TEMP TABLE w_dbsetup AS
    SELECT
    t_widget_user_parameter.oid as uuid,
    t_dashboard.code || '|' || t_chart.code as dashboard_widget_code,
    username,
    title,
    x,
    y,
    w,
    h,
    time_calc,
    visible,
    t_dashboard.code || '|' || t_dashboard_onglet.index as tab_code,
    max_cat,
    array_agg(DISTINCT t_indicateur_serie.code) as include_series_code
		FROM pmsi.t_widget_user_parameter
		JOIN pmsi.t_dashboard_widget ON t_dashboard_widget.oid = dashboard_widget_id
		JOIN pmsi.t_dashboard ON t_dashboard.oid= t_dashboard_widget.dashboard_id
    JOIN pmsi.t_chart on t_chart.oid = t_dashboard_widget.chart_id
		JOIN pmsi.t_dashboard_onglet on t_dashboard_onglet.oid = tab_id
		LEFT JOIN pmsi.t_indicateur_serie ON ARRAY[t_indicateur_serie.oid] <@ include_series_id
		WHERE username = 'cti'
		GROUP BY 1,2,3,4,5,6,7,8,9,10,11, t_dashboard_onglet.index
		ORDER BY 2, 6, 7 LIMIT 0
;

-- 2/3 : Peuplement de la table temporaire
INSERT INTO w_dbsetup(
    uuid,
    dashboard_widget_code,
    username,
    title,
    x,
    y,
    w,
    h,
    time_calc,
    visible,
    tab_code,
    max_cat,
    include_series_code
)
VALUES
('ed6dc64d-67dc-43ee-aa36-0cd4c8faf96a', 'CTI_CAS|CTI_CAT2ADA', 'cti', 'CAT2A par Domaine Activite', 2, 0, 4, 15, 'val', 't', 'CTI_CAS|1', 10, '{"CTI_CAT2AAMBU", "CTI_CAT2AHOSP"}'),
('4dbc1879-9b7e-484e-8e24-d11521cd85fb', 'CTI_CAS|CTI_CAT2AMOYDA', 'cti', 'CAT2A Moyen par Domaine Activite', 0, 15, 4, 12, 'val', 't', 'CTI_CAS|1', 5, '{"CTI_CAT2AAMBUMOY", "CTI_CAT2AHOSPMOY"}'),
('32d6e040-7365-4ac3-a92a-c438b5e2e891', 'CTI_CAS|CTI_INFO', 'cti', 'GHM', 0, 0, 2, 5, 'val', 't', 'CTI_CAS|1', 0, '{"CTI_GHM80"}'),
('b56adc4b-31ae-4f7f-bbb8-6fd3c891efb3', 'CTI_CAS|CTI_INFO', 'cti', 'DMS', 0, 5, 2, 5, 'val', 't', 'CTI_CAS|1', 0, '{"CTI_DMSHOSP"}'),
('c07d7267-ab42-4e01-8e38-835c20f763ed', 'CTI_CAS|CTI_INFO', 'cti', 'Séjours', 0, 10, 2, 5, 'val', 't', 'CTI_CAS|1', 0, '{"CTI_NBSEJGR"}'),
('0b4f34a5-5542-49aa-a5c6-4666077e3e6c', 'CTI_CAS|CTI_NBSEJ', 'cti', 'Nombre de sejours', 0, 27, 6, 10, 'val', 't', 'CTI_CAS|1', 8, '{"CTI_NBSEJFEMMES", "CTI_NBSEJHOMMES"}'),
('0a339d4b-5ef5-477d-9397-65626f076cbb', 'CTI_CAS|CTI_NBSEJCAS', 'cti', 'Séjours PMSI par CAS', 4, 15, 2, 12, 'val', 't', 'CTI_CAS|1', 0, '{"CTI_NBSEJ"}')
;

-- 3/3 : Màj de la table iCTI
DELETE FROM pmsi.t_widget_user_parameter WHERE username = 'cti';

INSERT INTO pmsi.t_widget_user_parameter(
	oid, 
	dashboard_widget_id,
	username,
	title,
	x,
	y,
	w,
	h,
	time_calc,
	visible,
	tab_id,
	max_cat,
	include_series_id
)
	SELECT
		w_dbsetup.uuid,
		t_dashboard_widget.oid,
		w_dbsetup.username,
		w_dbsetup.title,
		w_dbsetup.x,
		w_dbsetup.y,
		w_dbsetup.w,
		w_dbsetup.h,
		w_dbsetup.time_calc,
		w_dbsetup.visible,
		pmsi.t_dashboard_onglet.oid,
		w_dbsetup.max_cat,
		case when array_agg(DISTINCT t_indicateur_serie.oid) = ARRAY[NULL]::bigint[] then ARRAY[]::bigint[] else array_agg(DISTINCT t_indicateur_serie.oid) end
	FROM w_dbsetup
	JOIN pmsi.t_dashboard ON t_dashboard.code = SPLIT_PART(dashboard_widget_code, '|', 1)
	JOIN pmsi.t_chart on t_chart.code = SPLIT_PART(dashboard_widget_code, '|', 2)
	JOIN pmsi.t_dashboard_widget ON t_dashboard_widget.dashboard_id = t_dashboard.oid  and t_dashboard_widget.chart_id = t_chart.oid
	JOIN pmsi.t_dashboard_onglet ON t_dashboard_onglet.dashboard_id = t_dashboard.oid and t_dashboard_onglet.index = SPLIT_PART(tab_code, '|', 2)
	LEFT JOIN pmsi.t_indicateur_serie ON ARRAY[t_indicateur_serie.code] <@ include_series_code
	GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12
	ORDER BY 1
;