
set serveroutput on
set long 999999
set linesize 200
set pagesize 5000

/* --------------------------------

CREATE COLLECTION TABLE

-------------------------------- */ 
DROP TABLE IF EXISTS parsed_markdown_files;

CREATE JSON COLLECTION TABLE IF NOT EXISTS parsed_markdown_files;



/* --------------------------------

CONVERT MD TO JSON 

-------------------------------- */ 
exec CONVERT_MDFILES_TO_JSON


/* --------------------------------

CONSULTAS

-------------------------------- */ 
-- json collection
SELECT json_serialize(data returning clob pretty) AS json_data 
FROM parsed_markdown_files;


-- merchants
SELECT docs.category, 
       docs.name, 
       docs.payment_processor
FROM parsed_markdown_files,
     JSON_TABLE(data, '$.comercios[*]'
                COLUMNS (
                  name              VARCHAR2(4000) PATH '$.name',
                  category          VARCHAR2(4000) PATH '$.category',
                  payment_processor VARCHAR2(20)   PATH '$.payment_processor'
                )) docs
ORDER BY category, name;

-- beneficios por nivel
SELECT docs.nivel, 
       docs.percentual_reintegro, 
       docs.monto_compra_minimo_gs,
       docs.reintegro_maximo_gs
FROM parsed_markdown_files,
     JSON_TABLE(data, '$.beneficios_por_nivel[*]'
                COLUMNS (
                  nivel                   VARCHAR2(20) PATH '$.nivel',
                  percentual_reintegro    NUMBER       PATH '$.percentual_reintegro',
                  monto_compra_minimo_gs  NUMBER       PATH '$.monto_compra_minimo_gs',
                  reintegro_maximo_gs     NUMBER       PATH '$.reintegro_maximo_gs'
                )) docs
ORDER BY 1;

-- Tabela de benefícios
SELECT nivel, percentual_reintegro || '%' AS pct,
       TO_CHAR(monto_compra_minimo_gs, '999G999G999') AS monto_min,
       TO_CHAR(reintegro_maximo_gs, '999G999G999') AS reint_max
  FROM parsed_markdown_files t,
       JSON_TABLE(t.data, '$.beneficios_por_nivel[*]'
         COLUMNS (
           nivel                   VARCHAR2(20) PATH '$.nivel',
           percentual_reintegro    NUMBER       PATH '$.percentual_reintegro',
           monto_compra_minimo_gs  NUMBER       PATH '$.monto_compra_minimo_gs',
           reintegro_maximo_gs     NUMBER       PATH '$.reintegro_maximo_gs'
         ))
 ORDER BY nivel DESC;

-- Comércios por categoria
SELECT category, payment_processor, COUNT(*) AS qtd
  FROM parsed_markdown_files t,
       JSON_TABLE(t.data, '$.comercios[*]'
         COLUMNS (
           category          VARCHAR2(200) PATH '$.category',
           payment_processor VARCHAR2(20)  PATH '$.payment_processor'
         ))
 GROUP BY category, payment_processor
 ORDER BY qtd DESC;


-- JSON Dot-Notation
SELECT 
    docs.data.promocao.titulo.string() as  titulo,
    docs.data.promocao.vigencia.data_inicio || ' a ' || docs.data.promocao.vigencia.data_fim as vigencia,
    docs.data.medios_pago.rede as rede,
    docs.data.medios_pago.qr_permitido as qr_permitido,
    docs.data.cuotas.maximo_sin_intereses as max_sin_intereses,
    docs.data.acreditacao_reintegro.prazo_dias_habiles as prazo_dias_habiles
FROM parsed_markdown_files docs;


