-- cleanup_delete.sql
-- 按 Phase 0 新规则删除误判文献（分批删除，避免长锁）
-- 依赖：/tmp/mtrees2025.bin 已在容器内

BEGIN;

-- 1. 加载 mtrees
CREATE TEMP TABLE tmp_mtrees_raw (line TEXT);
COPY tmp_mtrees_raw FROM '/tmp/mtrees2025.bin' WITH (FORMAT text);

CREATE TEMP TABLE tmp_mtrees AS
SELECT
  TRIM(SPLIT_PART(line, ';', 1)) AS desc_name,
  TRIM(SPLIT_PART(line, ';', 2)) AS tree_number
FROM tmp_mtrees_raw;

CREATE TEMP TABLE tmp_c04 AS
SELECT DISTINCT LOWER(desc_name) AS name FROM tmp_mtrees
WHERE tree_number LIKE 'C04.%' OR tree_number = 'C04';

CREATE TEMP TABLE tmp_kw AS
SELECT unnest(ARRAY[
    'cancer','carcinoma','tumor','tumour','neoplasm','malignancy',
    'leukemia','leukaemia','lymphoma','sarcoma','melanoma',
    'glioma','glioblastoma','myeloma','neuroblastoma',
    'chemotherapy','radiotherapy','immunotherapy','oncolog',
    'metasta','antineoplastic'
]) AS kw;

-- 2. 收集待删除 ID（带序号用于分批）
CREATE TEMP TABLE tmp_to_delete AS
SELECT gl.id, row_number() OVER () AS rn
FROM global_literature gl
WHERE
  CASE
    WHEN gl.mesh_headings IS NOT NULL AND jsonb_array_length(gl.mesh_headings::jsonb) > 0 THEN
      NOT EXISTS (
        SELECT 1 FROM jsonb_array_elements(gl.mesh_headings::jsonb) mh
        JOIN tmp_c04 c04 ON c04.name = LOWER(mh->>'descriptor')
      )
    ELSE
      NOT (
        EXISTS (SELECT 1 FROM tmp_kw WHERE POSITION(kw IN LOWER(COALESCE(gl.title, ''))) > 0)
        OR
        (SELECT COUNT(*) FROM tmp_kw WHERE POSITION(kw IN LOWER(COALESCE(gl.title,'') || ' ' || COALESCE(gl.abstract,''))) > 0) >= 2
      )
  END;

SELECT 'To be deleted: ' || COUNT(*) || ' records' AS info FROM tmp_to_delete;

COMMIT;

-- 3. 分批删除（每批 500 条）
DO $$
DECLARE
    batch_size CONSTANT INT := 500;
    total INT;
    deleted INT := 0;
    batch INT;
BEGIN
    SELECT COUNT(*) INTO total FROM tmp_to_delete;
    RAISE NOTICE 'Starting deletion of % records in batches of %', total, batch_size;

    FOR batch IN 0..(total / batch_size) LOOP
        DELETE FROM global_literature gl
        WHERE gl.id IN (
            SELECT t.id FROM tmp_to_delete t
            WHERE t.rn > batch * batch_size AND t.rn <= (batch + 1) * batch_size
        );

        deleted := deleted + batch_size;
        IF batch % 50 = 0 THEN
            RAISE NOTICE 'Progress: % / %', LEAST(deleted, total), total;
        END IF;
    END LOOP;

    RAISE NOTICE 'Delete complete: % records removed', total;
END $$;

-- 4. 验证
SELECT 'After cleanup: ' || COUNT(*) || ' records remain' AS info FROM global_literature;
