-- +goose Up
-- +goose StatementBegin
CREATE OR REPLACE PROCEDURE PRC_FLASH_CARGA_RESUMO_CONTRIBUICAO (
    p_data_inicial IN DATE,
    p_data_final   IN DATE
) AS
BEGIN
    DELETE FROM TB_FLASH_NODO_RESUMO_CONTRIBUICAO
     WHERE data_referencia BETWEEN p_data_inicial AND p_data_final;

    INSERT INTO TB_FLASH_NODO_RESUMO_CONTRIBUICAO (
        id_resumo, id_nodo, data_referencia, ano_referencia, mes_referencia, dia_referencia,
        nro_tickets, vlr_imp_contribuicao, qtde_contribuicao,
        vlr_contribuicao, vl_ticket_medio,
        valor_meta_propria, valor_meta_filhos, valor_meta_efetiva,
        percentual_meta_propria, percentual_meta_filhos, percentual_meta_efetiva,
        desvio, percentual_atingimento, data_apuracao
    )
    WITH datas AS (
        SELECT TRUNC(p_data_inicial) + LEVEL - 1 AS data_referencia
          FROM dual
        CONNECT BY TRUNC(p_data_inicial) + LEVEL - 1 <= TRUNC(p_data_final)
    ),
    contribuicao_direta AS (
        SELECT f.id_nodo, TRUNC(f.data_referencia) AS data_referencia,
               f.ano_referencia, f.mes_referencia, f.dia_referencia,
               SUM(f.nro_tickets)          AS nro_tickets,
               SUM(f.vlr_imp_contribuicao) AS vlr_imp_contribuicao,
               SUM(f.qtde_contribuicao)    AS qtde_contribuicao,
               SUM(f.vlr_contribuicao)     AS vlr_contribuicao
          FROM TB_FLASH_FATO_CONTRIBUICAO f
         WHERE f.data_referencia BETWEEN p_data_inicial AND p_data_final
         GROUP BY f.id_nodo, TRUNC(f.data_referencia), f.ano_referencia, f.mes_referencia, f.dia_referencia
    ),
    contribuicao_hierarquia AS (
        SELECT a.id_nodo_ancestral AS id_nodo,
               d.data_referencia,
               EXTRACT(YEAR FROM d.data_referencia) AS ano_referencia,
               EXTRACT(MONTH FROM d.data_referencia) AS mes_referencia,
               EXTRACT(DAY FROM d.data_referencia) AS dia_referencia,
               SUM(NVL(cd.nro_tickets, 0))          AS nro_tickets,
               SUM(NVL(cd.vlr_imp_contribuicao, 0)) AS vlr_imp_contribuicao,
               SUM(NVL(cd.qtde_contribuicao, 0))    AS qtde_contribuicao,
               SUM(NVL(cd.vlr_contribuicao, 0))     AS vlr_contribuicao
          FROM (
                SELECT CONNECT_BY_ROOT n.id_nodo AS id_nodo_ancestral,
                       n.id_nodo AS id_nodo_descendente
                  FROM TB_FLASH_NODO n
                CONNECT BY PRIOR n.id_nodo = n.id_nodo_pai
               ) a
          CROSS JOIN datas d
          LEFT JOIN contribuicao_direta cd
            ON cd.id_nodo = a.id_nodo_descendente
           AND cd.data_referencia = d.data_referencia
         GROUP BY a.id_nodo_ancestral, d.data_referencia
    ),
    meta_propria_diaria AS (
        SELECT m.id_nodo, m.data_referencia, m.ano_referencia, m.mes_referencia, m.dia_referencia,
               MAX(m.percentual_meta) AS percentual_meta_propria,
               SUM(m.valor_meta) AS valor_meta_propria
          FROM TB_FLASH_META_CONTRIBUICAO m
         WHERE m.data_referencia BETWEEN p_data_inicial AND p_data_final
         GROUP BY m.id_nodo, m.data_referencia, m.ano_referencia, m.mes_referencia, m.dia_referencia
    ),
    meta_filhos_diaria AS (
        SELECT pai.id_nodo, mpd.data_referencia, mpd.ano_referencia, mpd.mes_referencia, mpd.dia_referencia,
               SUM(mpd.valor_meta_propria) AS valor_meta_filhos
          FROM TB_FLASH_NODO pai
          JOIN TB_FLASH_NODO filho ON filho.id_nodo_pai = pai.id_nodo
          JOIN meta_propria_diaria mpd ON mpd.id_nodo = filho.id_nodo
         GROUP BY pai.id_nodo, mpd.data_referencia, mpd.ano_referencia, mpd.mes_referencia, mpd.dia_referencia
    ),
    base AS (
        SELECT n.id_nodo, d.data_referencia,
               EXTRACT(YEAR FROM d.data_referencia) AS ano_referencia,
               EXTRACT(MONTH FROM d.data_referencia) AS mes_referencia,
               EXTRACT(DAY FROM d.data_referencia) AS dia_referencia
          FROM TB_FLASH_NODO n
          CROSS JOIN datas d
    )
    SELECT SEQ_TB_FLASH_NODO_RESUMO_CONTRIBUICAO.NEXTVAL,
           b.id_nodo, b.data_referencia, b.ano_referencia, b.mes_referencia, b.dia_referencia,
           ch.nro_tickets,
           ch.vlr_imp_contribuicao,
           ch.qtde_contribuicao,
           ch.vlr_contribuicao,
           CASE WHEN NVL(ch.nro_tickets, 0) > 0 THEN ch.vlr_contribuicao / ch.nro_tickets ELSE NULL END,
           mpd.valor_meta_propria,
           mfd.valor_meta_filhos,
           CASE WHEN mpd.valor_meta_propria IS NOT NULL THEN mpd.valor_meta_propria ELSE mfd.valor_meta_filhos END,
           mpd.percentual_meta_propria,
           CAST(NULL AS NUMBER(10,4)),
           CASE WHEN mpd.percentual_meta_propria IS NOT NULL THEN mpd.percentual_meta_propria ELSE NULL END,
           NVL(ch.vlr_contribuicao, 0) - NVL(CASE WHEN mpd.valor_meta_propria IS NOT NULL THEN mpd.valor_meta_propria ELSE mfd.valor_meta_filhos END, 0),
           CASE
             WHEN NVL(CASE WHEN mpd.valor_meta_propria IS NOT NULL THEN mpd.valor_meta_propria ELSE mfd.valor_meta_filhos END, 0) = 0 THEN NULL
             ELSE (NVL(ch.vlr_contribuicao, 0) / CASE WHEN mpd.valor_meta_propria IS NOT NULL THEN mpd.valor_meta_propria ELSE mfd.valor_meta_filhos END) * 100
           END,
           SYSDATE
      FROM base b
      LEFT JOIN contribuicao_hierarquia ch ON ch.id_nodo = b.id_nodo AND ch.data_referencia = b.data_referencia
      LEFT JOIN meta_propria_diaria mpd ON mpd.id_nodo = b.id_nodo AND mpd.data_referencia = b.data_referencia
      LEFT JOIN meta_filhos_diaria mfd ON mfd.id_nodo = b.id_nodo AND mfd.data_referencia = b.data_referencia;

    COMMIT;
END;
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DECLARE
    v_count NUMBER;
BEGIN
    SELECT COUNT(*) INTO v_count FROM user_objects WHERE object_name = 'PRC_FLASH_CARGA_RESUMO_CONTRIBUICAO' AND object_type = 'PROCEDURE';
    IF v_count > 0 THEN
        EXECUTE IMMEDIATE 'DROP PROCEDURE PRC_FLASH_CARGA_RESUMO_CONTRIBUICAO';
    END IF;
END;
-- +goose StatementEnd
