-- Per-UAT KPI materialized view powering /harta v2 multi-metric choropleth.
-- Columns:
--   total_contracts, total_value, distinct_suppliers
--   direct_pct          — share of value awarded via direct procurement (type='da')
--   framework_pct       — share via framework agreements
--   hhi_suppliers       — Herfindahl-Hirschman index 0..10000 (DOJ thresholds: <1500 ok, 1500-2500 moderate, >2500 concentrated)
--   top_supplier_share  — biggest single-supplier dependency 0..1
--   q4_spike            — Q4 value / (yearly_avg_quarter) for last full year; >1.5 = spike, NULL if no data
--
-- Refresh: weekly cron — REFRESH MATERIALIZED VIEW CONCURRENTLY seap.uat_kpi;
-- Idempotent: safe to re-run.

BEGIN;

DROP MATERIALIZED VIEW IF EXISTS seap.uat_kpi CASCADE;

CREATE MATERIALIZED VIEW seap.uat_kpi AS
WITH base AS (
  SELECT
    a.authority_siruta AS siruta,
    a.authority_cui,
    a.supplier_cui,
    a.type,
    a.awarded_value,
    a.publication_date,
    a.framework_agreement
  FROM seap.announcements a
  WHERE a.authority_siruta IS NOT NULL
),
uat_totals AS (
  SELECT
    siruta,
    COUNT(*)::int AS total_contracts,
    COALESCE(SUM(awarded_value), 0)::numeric(20,2) AS total_value,
    COALESCE(SUM(awarded_value) FILTER (WHERE type = 'da'), 0)::numeric(20,2) AS direct_value,
    COALESCE(SUM(awarded_value) FILTER (WHERE framework_agreement = true), 0)::numeric(20,2) AS framework_value,
    COUNT(DISTINCT supplier_cui)::int AS distinct_suppliers
  FROM base
  GROUP BY siruta
),
supplier_shares AS (
  SELECT
    siruta,
    supplier_cui,
    SUM(awarded_value) / NULLIF(SUM(SUM(awarded_value)) OVER (PARTITION BY siruta), 0) AS ratio
  FROM base
  WHERE supplier_cui IS NOT NULL AND awarded_value IS NOT NULL
  GROUP BY siruta, supplier_cui
),
hhi_calc AS (
  SELECT
    siruta,
    COALESCE(SUM(POWER(ratio, 2)) * 10000, 0) AS hhi,
    COALESCE(MAX(ratio), 0) AS top_supplier_share
  FROM supplier_shares
  GROUP BY siruta
),
last_full_year AS (
  SELECT extract(year from now()) - 1 AS yr
),
q4_data AS (
  SELECT
    siruta,
    COALESCE(SUM(awarded_value) FILTER (WHERE extract(quarter FROM publication_date) = 4), 0)::numeric AS q4_value,
    COALESCE(SUM(awarded_value), 0)::numeric AS yearly_value
  FROM base
  WHERE extract(year FROM publication_date) = (SELECT yr FROM last_full_year)
  GROUP BY siruta
)
SELECT
  ut.siruta,
  ut.total_contracts,
  ut.total_value,
  ut.distinct_suppliers,
  CASE WHEN ut.total_value > 0 THEN ut.direct_value / ut.total_value ELSE 0 END AS direct_pct,
  CASE WHEN ut.total_value > 0 THEN ut.framework_value / ut.total_value ELSE 0 END AS framework_pct,
  COALESCE(hh.hhi, 0)::numeric(10,2) AS hhi_suppliers,
  COALESCE(hh.top_supplier_share, 0)::numeric(8,4) AS top_supplier_share,
  CASE WHEN q4.yearly_value > 0 THEN q4.q4_value / (q4.yearly_value / 4) ELSE NULL END AS q4_spike
FROM uat_totals ut
LEFT JOIN hhi_calc hh ON hh.siruta = ut.siruta
LEFT JOIN q4_data q4 ON q4.siruta = ut.siruta;

CREATE UNIQUE INDEX IF NOT EXISTS idx_uat_kpi_pk ON seap.uat_kpi(siruta);
CREATE INDEX IF NOT EXISTS idx_uat_kpi_value ON seap.uat_kpi(total_value DESC NULLS LAST);
CREATE INDEX IF NOT EXISTS idx_uat_kpi_direct ON seap.uat_kpi(direct_pct DESC) WHERE total_contracts > 5;
CREATE INDEX IF NOT EXISTS idx_uat_kpi_hhi ON seap.uat_kpi(hhi_suppliers DESC) WHERE total_contracts > 5;

COMMIT;

-- Refresh helper (idempotent)
CREATE OR REPLACE FUNCTION seap.refresh_uat_kpi() RETURNS void LANGUAGE sql AS $$
  REFRESH MATERIALIZED VIEW CONCURRENTLY seap.uat_kpi;
$$;
