-- PR-DocSrv-Hier-Replace-Diagnose-1 c4: 측정 전용 view (additive, droppable, in_corpus 무관)
-- prehier = pre-hier baseline (legacy + null-source). hier_sim_* = post-replace 시뮬(doc 단위 fallback).
-- clean = childless-tiny(<30자) leaf 제외 (A1 held-out 발견). kept-leaf = is_leaf AND (len>=30 OR has child).

DROP VIEW IF EXISTS corpus_chunks_prehier;
DROP VIEW IF EXISTS corpus_chunks_hier_sim_raw;
DROP VIEW IF EXISTS corpus_chunks_hier_sim_clean;

CREATE VIEW corpus_chunks_prehier AS
  SELECT * FROM document_chunks
  WHERE source_type IS DISTINCT FROM 'hier_section' AND embedding IS NOT NULL;

CREATE VIEW corpus_chunks_hier_sim_raw AS
  SELECT * FROM document_chunks dc
  WHERE dc.embedding IS NOT NULL AND (
    (dc.source_type = 'hier_section' AND dc.is_leaf = true)
    OR (dc.source_type IS DISTINCT FROM 'hier_section'
        AND NOT EXISTS (SELECT 1 FROM document_chunks h
                        WHERE h.doc_id = dc.doc_id AND h.source_type = 'hier_section'
                          AND h.is_leaf = true AND h.embedding IS NOT NULL))
  );

CREATE VIEW corpus_chunks_hier_sim_clean AS
  SELECT * FROM document_chunks dc
  WHERE dc.embedding IS NOT NULL AND (
    -- kept hier leaf: is_leaf AND NOT childless-tiny
    (dc.source_type = 'hier_section' AND dc.is_leaf = true
       AND (length(trim(dc.text)) >= 30
            OR EXISTS (SELECT 1 FROM document_chunks ch WHERE ch.parent_id = dc.id)))
    -- legacy fallback: doc 에 kept(clean) hier leaf 가 하나도 없을 때만
    OR (dc.source_type IS DISTINCT FROM 'hier_section'
        AND NOT EXISTS (SELECT 1 FROM document_chunks h
                        WHERE h.doc_id = dc.doc_id AND h.source_type = 'hier_section'
                          AND h.is_leaf = true AND h.embedding IS NOT NULL
                          AND (length(trim(h.text)) >= 30
                               OR EXISTS (SELECT 1 FROM document_chunks ch2 WHERE ch2.parent_id = h.id))))
  );
