-- 数据质量报告 SQL
-- 用于基线导入后的数据质量检查

-- 1. 总览
SELECT '1. 总览' as section;
SELECT '总文献数' as metric, COUNT(*)::text as value FROM global_literature
UNION ALL
SELECT '有 MeSH', COUNT(*)::text FROM global_literature WHERE mesh_headings IS NOT NULL AND jsonb_array_length(mesh_headings::jsonb) > 0
UNION ALL
SELECT '无 MeSH', COUNT(*)::text FROM global_literature WHERE mesh_headings IS NULL OR jsonb_array_length(mesh_headings::jsonb) = 0
UNION ALL
SELECT '有标签(global_literature_tags)', COUNT(DISTINCT gl.id)::text FROM global_literature gl JOIN global_literature_tags glt ON glt.literature_id = gl.id
UNION ALL
SELECT '有全文(PMC)', COUNT(*)::text FROM global_literature WHERE is_oa = true
UNION ALL
SELECT '有AI摘要', COUNT(*)::text FROM global_literature WHERE ai_summary IS NOT NULL AND ai_summary->>'one_liner' IS NOT NULL
UNION ALL
SELECT '有研究设计', COUNT(*)::text FROM global_literature WHERE study_design IS NOT NULL AND study_design->>'primary' IS NOT NULL
UNION ALL
SELECT '有PICO', COUNT(*)::text FROM global_literature WHERE pico IS NOT NULL
UNION ALL
SELECT '有引用次数(>0)', COUNT(*)::text FROM global_literature WHERE cited_by_count > 0;

-- 2. 来源分布
SELECT '2. 来源分布' as section;
SELECT source, COUNT(*)::text as cnt FROM global_literature GROUP BY source ORDER BY COUNT(*) DESC;

-- 3. 标引状态分布
SELECT '3. 标引状态分布' as section;
SELECT COALESCE(citation_status, 'NULL') as status, COUNT(*)::text as cnt FROM global_literature GROUP BY citation_status ORDER BY COUNT(*) DESC;

-- 4. C04 标签覆盖（通过 global_tag_tree_numbers）
SELECT '4. C04 标签覆盖' as section;
SELECT COUNT(DISTINCT gl.id)::text as has_c04_tag FROM global_literature gl
JOIN global_literature_tags glt ON glt.literature_id = gl.id
JOIN global_tag_tree_numbers gttn ON gttn.tag_id = glt.tag_id AND gttn.tree_category = 'C04';

-- 5. 打标完整性
SELECT '5. 打标完整性' as section;
WITH mesh_count AS (
  SELECT gl.id, jsonb_array_length(gl.mesh_headings::jsonb) AS total_mesh
  FROM global_literature gl
  WHERE gl.mesh_headings IS NOT NULL AND jsonb_array_length(gl.mesh_headings::jsonb) > 0
),
tag_count AS (
  SELECT gl.id, COUNT(glt.tag_id) AS total_tags
  FROM global_literature gl
  JOIN global_literature_tags glt ON glt.literature_id = gl.id
  WHERE gl.mesh_headings IS NOT NULL AND jsonb_array_length(gl.mesh_headings::jsonb) > 0
  GROUP BY gl.id
)
SELECT
  COUNT(*)::text as total_with_mesh,
  COUNT(tc.id)::text as has_at_least_one_tag,
  ROUND(COUNT(tc.id) * 100.0 / COUNT(*), 1)::text as pct_tagged
FROM mesh_count mc LEFT JOIN tag_count tc ON tc.id = mc.id;

-- 6. 年度分布（近10年）
SELECT '6. 年度分布(近10年)' as section;
SELECT pub_year::text, COUNT(*)::text as cnt FROM global_literature WHERE pub_year >= 2016 GROUP BY pub_year ORDER BY pub_year DESC;

-- 7. 引用次数分布
SELECT '7. 引用次数分布' as section;
SELECT range, cnt::text FROM (
  SELECT CASE
    WHEN cited_by_count = 0 THEN '0'
    WHEN cited_by_count BETWEEN 1 AND 10 THEN '1-10'
    WHEN cited_by_count BETWEEN 11 AND 100 THEN '11-100'
    WHEN cited_by_count BETWEEN 101 AND 1000 THEN '101-1000'
    ELSE '1001+'
  END as range,
  COUNT(*) as cnt
  FROM global_literature GROUP BY 1
) sub ORDER BY range;

-- 8. 标签来源分布
SELECT '8. 标签来源分布' as section;
SELECT source, is_active, COUNT(*)::text as cnt FROM global_tags GROUP BY source, is_active ORDER BY source;

-- 9. 语种分布
SELECT '9. 语种分布' as section;
SELECT language, COUNT(*)::text as cnt FROM global_literature GROUP BY language ORDER BY COUNT(*) DESC LIMIT 10;

-- 10. 有摘要率
SELECT '10. 文本覆盖率' as section;
SELECT '有摘要' as metric, COUNT(*)::text FROM global_literature WHERE abstract IS NOT NULL AND abstract != ''
UNION ALL
SELECT '有DOI', COUNT(*)::text FROM global_literature WHERE doi IS NOT NULL AND doi != ''
UNION ALL
SELECT '有keywords', COUNT(*)::text FROM global_literature WHERE keywords IS NOT NULL AND jsonb_array_length(keywords::jsonb) > 0
UNION ALL
SELECT '有grants', COUNT(*)::text FROM global_literature WHERE grants IS NOT NULL AND jsonb_array_length(grants::jsonb) > 0
UNION ALL
SELECT '有chemical_list', COUNT(*)::text FROM global_literature WHERE chemical_list IS NOT NULL AND jsonb_array_length(chemical_list::jsonb) > 0
UNION ALL
SELECT '有gene_symbols', COUNT(*)::text FROM global_literature WHERE gene_symbols IS NOT NULL AND jsonb_array_length(gene_symbols::jsonb) > 0;

-- 11. 期刊 Tier 分布
SELECT '11. 期刊 Tier 分布' as section;
SELECT COALESCE(gj.tier, 'unknown') as tier, COUNT(*)::text as cnt
FROM global_literature gl LEFT JOIN global_journals gj ON gj.issn = gl.journal_issn
GROUP BY gj.tier ORDER BY COUNT(*) DESC;

-- 12. 标签分类分布
SELECT '12. 标签分类分布' as section;
SELECT gt.tag_category, COUNT(DISTINCT glt.literature_id)::text as lit_count, COUNT(DISTINCT glt.tag_id)::text as tag_count
FROM global_literature_tags glt
JOIN global_tags gt ON gt.id = glt.tag_id
GROUP BY gt.tag_category ORDER BY COUNT(DISTINCT glt.literature_id) DESC;
