--\timing
--explain (analyze, buffers)
WITH
mseq AS (
    SELECT * FROM
    (
        VALUES
        ('01 - Jun',1)
        ,('02 - Jul',2)
        ,('03 - Aug',3)
        ,('04 - Sep',4)
        ,('05 - Oct',5)
        ,('06 - Nov',6)
        ,('07 - Dec',7)
        ,('08 - Jan',8)
        ,('09 - Feb',9)
        ,('10 - Mar',10)
        ,('11 - Apr',11)
        ,('12 - May',12)
    ) x(m,s)
)
-- select 'mseq', * from mseq
--
,base AS (
SELECT
    order_season
    ,order_month
    ,version
    ,iter
    ,part_descr
    ,bill_cust_descr
    ,ship_cust_descr
    ,SUM(units) units
    ,SUM(value_usd) value_usd
FROM
    rlarp.osm_pool
WHERE
    where_clause
    AND order_season IN (2025,2026)
GROUP BY
    order_season
    ,order_month
    ,version
    ,iter
    ,part_descr
    ,bill_cust_descr
    ,ship_cust_descr
)
-- select 'base', * from base
--
, basket1 AS (
    SELECT
        part_descr
        ,bill_cust_descr
        ,ship_cust_descr
        ,SUM(value_usd) value_usd
    FROM
        base
    GROUP BY
        part_descr
        ,bill_cust_descr
        ,ship_cust_descr
)
-- select 'basket1', * from basket1
--
,basket AS (
    SELECT
        part_descr
        ,bill_cust_descr
        ,ship_cust_descr
        ,CASE WHEN sum(value_usd) over () = 0 THEN 0 ELSE value_usd/sum(value_usd) over () END mix
    FROM
        basket1
    ORDER BY
        mix DESC
)
-- select 'basket', * from basket
--
,months AS (
    SELECT
        order_season
        ,version
        ,iter
        ,order_month
        ,mseq.s seq
        ,sum(units) units
        ,sum(value_usd) value_usd
    FROM
        base
        INNER JOIN mseq ON
            mseq.m = base.order_month
    GROUP BY
        order_season
        ,version
        ,iter
        ,order_month
        ,s
)
-- select 'months', * from months
--
,mpvt AS (
    SELECT
        order_month
        ,seq
        ,SUM(units) FILTER (WHERE order_season = 2025)                                                              "2025 qty"
        ,SUM(units) FILTER (WHERE order_season = 2026 AND iter IN ('copy','short ship','bad_ship','plan'))          "2026 base qty"
        ,SUM(units) FILTER (WHERE order_season = 2026 AND iter NOT IN ('copy','short ship','bad_ship','plan'))      "2026 adj qty"
        ,SUM(units) FILTER (WHERE order_season = 2026)                                                              "2026 tot qty"
        ,SUM(value_usd) FILTER (WHERE order_season = 2025)                                                          "2025 value_usd"
        ,SUM(value_usd) FILTER (WHERE order_season = 2026 AND iter IN ('copy','short ship','bad_ship','plan'))      "2026 base value_usd"
        ,SUM(value_usd) FILTER (WHERE order_season = 2026 AND iter NOT IN ('copy','short ship','bad_ship','plan'))  "2026 adj value_usd"
        ,SUM(value_usd) FILTER (WHERE order_season = 2026)                                                          "2026 tot value_usd"
    FROM
        months
    GROUP BY
        order_month
        ,seq
    ORDER BY
        seq ASC
)
-- select 'mpvt', * from mpvt
--
,mlist AS (
    SELECT
        mseq.m order_month
        ,"2025 qty"
        ,"2026 base qty"
        ,"2026 adj qty"
        ,"2026 tot qty"
        ,"2025 value_usd"
        ,"2026 base value_usd"
        ,"2026 adj value_usd"
        ,"2026 tot value_usd"
    FROM
        mseq
        LEFT OUTER JOIN mpvt ON
            mpvt.order_month = mseq.m
    ORDER BY
        mseq.s ASC
)
-- select 'mlist', * from mlist
--
,totals AS (
    SELECT
        order_season
        ,version
        ,iter
        ,sum(units) units
        ,sum(value_usd) value_usd
    FROM
        months
    GROUP BY
        order_season
        ,version
        ,iter
)
-- select 'totals', * from totals
--
,tags AS (
    SELECT DISTINCT
        doc->>'tag' tag
    FROM
        rlarp.osm_log
    WHERE
        doc ? 'tag'
        AND id <> 1
    UNION
    SELECT * FROM
        (VALUES
            ('price'),
            ('volume')
        ) x(tag)
)
-- select 'tags', * from tags
--
,custs AS (
    SELECT
        bill_cust_descr
        ,ship_cust_descr
        ,SUM(value_usd) value_usd
    FROM
        base
    GROUP BY
        bill_cust_descr
        ,ship_cust_descr
)
,alldates AS (
    SELECT
        bill_cust_descr
        ,ship_cust_descr
        ,promo
        ,terms
        ,r_currency
        -- ,order_month
        -- ,mseq.s seq
        ,order_date
        -- ,request_date
        ,ship_date
        -- ,ship_month
        -- ,sum(value_usd) value_usd_net
        ,sum(pounds) pounds_net
        -- ,sum(value_usd) FILTER (WHERE iter IN ('copy','actuals','actuals_plug')) value_usd_baseline
        ,sum(pounds)    FILTER (WHERE iter IN ('copy','actuals','actuals_plug')) pounds_baseline
    FROM
        rlarp.osm_pool
        LEFT OUTER JOIN mseq ON
            mseq.m = order_month
    WHERE
        where_clause
        -----------------scenario----------------------------
        -- AND version IN ('b26')
        AND order_season = 2026
        -----------------additional params-------------------
        AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
        AND order_date <= ship_date
    GROUP BY
        bill_cust_descr
        ,ship_cust_descr
        ,promo
        ,terms
        ,r_currency
        -- ,order_month
        -- ,mseq.s seq
        ,order_date
        -- ,request_date
        ,ship_date
        -- ,ship_month
    -- HAVING
    --     sum(value_usd) <> 0
)
,date_mix as (
    SELECT 
        bill_cust_descr
        ,ship_cust_descr
        ,promo
        ,terms
        ,r_currency
        ,order_date
        ,ship_date
        ,CASE WHEN sum(pounds_net) OVER ()      = 0 THEN 0 ELSE round(pounds_net      / sum(pounds_net)      OVER (),5) END fc_mix
        ,CASE WHEN sum(pounds_baseline) OVER () = 0 THEN 0 ELSE round(pounds_baseline / sum(pounds_baseline) OVER (),5) END baseline_mix
    FROM 
        alldates
    ORDER BY 
        order_date ASC
        ,ship_date ASC
)
,part_mix as (
    SELECT 
         part
        ,sum(units) qty
        ,ROUND(CASE WHEN sum(units) = 0 THEN 0 ELSE sum(value_usd)/sum(units) END,5) price
        ,sum(pounds) pounds
    FROM 
        rlarp.osm_pool
    WHERE 
        where_clause
        AND version IN ('b26','b26-act')
    GROUP BY 
        part
)
-- select 'custs', * from custs
--
SELECT
    jsonb_build_object(
        'months'
        ,(SELECT jsonb_agg(row_to_json(months)::jsonb) FROM months)
        ,'mpvt'
        ,(SELECT jsonb_agg(row_to_json(mlist)::jsonb) FROM mlist)
        ,'basket'
        ,(SELECT jsonb_agg(row_to_json(basket)::jsonb) FROM basket)
        ,'totals'
        ,(SELECT jsonb_agg(row_to_json(totals)::jsonb) FROM totals)
        ,'tags'
        ,(SELECT jsonb_agg(tag) FROM tags)
        ,'customers'
        ,(SELECT jsonb_agg(row_to_json(custs)::jsonb) FROM custs)
        ,'date_mix'
        ,(SELECT jsonb_agg(row_to_json(date_mix)::jsonb) FROM date_mix)
        ,'part_mix'
        ,(SELECT jsonb_agg(row_to_json(part_mix)::jsonb) FROM part_mix)
    ) package
