WITH base AS (
  SELECT
    "Symbol"               AS symbol,
    "ExpirationDate"::date AS expiry,
    UPPER("CallPut")       AS callput,
    "Strike"::float        AS strike,
    "Spot"::float          AS spot,      -- replace/join if missing
    "Volume"::float        AS vol,
    "OI"::float            AS oi
  FROM public."OptionsFlow_monthly"
  WHERE "ExpirationDate" BETWEEN CURRENT_DATE AND CURRENT_DATE + INTERVAL '30 days'
),
range_filtered AS (
  SELECT *
  FROM base
  WHERE strike BETWEEN spot * 0.9 AND spot * 1.1
),
side_calc AS (
  SELECT
    symbol,
    SUM(CASE WHEN callput = 'CALL' THEN vol ELSE 0 END) AS call_1m_vol,
    SUM(CASE WHEN callput = 'PUT'  THEN vol ELSE 0 END) AS put_1m_vol,
    SUM(CASE WHEN callput = 'CALL' THEN oi  ELSE 0 END) AS call_1m_oi,
    SUM(CASE WHEN callput = 'PUT'  THEN oi  ELSE 0 END) AS put_1m_oi
  FROM range_filtered
  GROUP BY symbol
),
meta AS (
  SELECT
    symbol,
    call_1m_vol, put_1m_vol, call_1m_oi, put_1m_oi,
    (call_1m_oi + put_1m_oi) AS tot_oi,
    CASE WHEN call_1m_oi >  put_1m_oi THEN 'CALL'
         WHEN call_1m_oi <  put_1m_oi THEN 'PUT'
         ELSE 'TIE' END AS dom_oi,
    CASE WHEN call_1m_vol > put_1m_vol THEN 'CALL'
         WHEN call_1m_vol < put_1m_vol THEN 'PUT'
         ELSE 'TIE' END AS dom_vol
  FROM side_calc
),
pretty AS (
  SELECT
    symbol,
    -- K/M/B formatting
    CASE
      WHEN call_1m_vol >= 1e9 THEN to_char(((call_1m_vol/1e9)::numeric), 'FM999,999,990.0') || 'B'
      WHEN call_1m_vol >= 1e6 THEN to_char(((call_1m_vol/1e6)::numeric), 'FM999,999,990.0') || 'M'
      WHEN call_1m_vol >= 1e3 THEN to_char(((call_1m_vol/1e3)::numeric), 'FM999,999,990.0') || 'K'
      ELSE to_char((call_1m_vol::numeric), 'FM999,999,990')
    END AS "Call_1M_Vol",
    CASE
      WHEN put_1m_vol >= 1e9 THEN to_char(((put_1m_vol/1e9)::numeric), 'FM999,999,990.0') || 'B'
      WHEN put_1m_vol >= 1e6 THEN to_char(((put_1m_vol/1e6)::numeric), 'FM999,999,990.0') || 'M'
      WHEN put_1m_vol >= 1e3 THEN to_char(((put_1m_vol/1e3)::numeric), 'FM999,999,990.0') || 'K'
      ELSE to_char((put_1m_vol::numeric), 'FM999,999,990')
    END AS "Put_1M_Vol",
    CASE
      WHEN call_1m_oi >= 1e9 THEN to_char(((call_1m_oi/1e9)::numeric), 'FM999,999,990.0') || 'B'
      WHEN call_1m_oi >= 1e6 THEN to_char(((call_1m_oi/1e6)::numeric), 'FM999,999,990.0') || 'M'
      WHEN call_1m_oi >= 1e3 THEN to_char(((call_1m_oi/1e3)::numeric), 'FM999,999,990.0') || 'K'
      ELSE to_char((call_1m_oi::numeric), 'FM999,999,990')
    END AS "Call_1M_OI",
    CASE
      WHEN put_1m_oi >= 1e9 THEN to_char(((put_1m_oi/1e9)::numeric), 'FM999,999,990.0') || 'B'
      WHEN put_1m_oi >= 1e6 THEN to_char(((put_1m_oi/1e6)::numeric), 'FM999,999,990.0') || 'M'
      WHEN put_1m_oi >= 1e3 THEN to_char(((put_1m_oi/1e3)::numeric), 'FM999,999,990.0') || 'K'
      ELSE to_char((put_1m_oi::numeric), 'FM999,999,990')
    END AS "Put_1M_OI",

    -- Highlight rules (dominance + divergence)
    CASE
      WHEN dom_oi = 'CALL' AND dom_vol = 'PUT' THEN '⚠️ OI CALL↑ / VOL PUT↑'
      WHEN dom_oi = 'PUT'  AND dom_vol = 'CALL' THEN '⚠️ OI PUT↑ / VOL CALL↑'
      WHEN dom_oi = 'CALL' AND call_1m_oi >= put_1m_oi * 1.3 THEN 'CALL 🚀'
      WHEN dom_oi = 'PUT'  AND put_1m_oi  >= call_1m_oi * 1.3 THEN 'PUT 🛑'
      ELSE dom_oi
    END AS "Highlight",

    -- sort helpers
    (CASE WHEN dom_oi <> dom_vol AND dom_oi <> 'TIE' AND dom_vol <> 'TIE' THEN 1 ELSE 0 END) AS diverge,
    tot_oi,
    GREATEST(call_1m_oi, put_1m_oi) AS big_side_oi
  FROM meta
)
SELECT
  symbol,
  "Call_1M_Vol",
  "Put_1M_Vol",
  "Call_1M_OI",
  "Put_1M_OI",
  "Highlight"
FROM pretty
-- put divergence first, then overall liquidity:
ORDER BY diverge DESC, tot_oi DESC, big_side_oi DESC, symbol
LIMIT 100;
