-- ============================================
-- 扩展 parse_tasks 表，支持多阶段进度跟踪
-- ============================================

-- 添加各阶段状态字段
ALTER TABLE parse_tasks ADD COLUMN IF NOT EXISTS parse_status VARCHAR(20) DEFAULT 'pending';
ALTER TABLE parse_tasks ADD COLUMN IF NOT EXISTS parse_progress INTEGER DEFAULT 0;

ALTER TABLE parse_tasks ADD COLUMN IF NOT EXISTS rag_status VARCHAR(20) DEFAULT 'pending';
ALTER TABLE parse_tasks ADD COLUMN IF NOT EXISTS rag_progress INTEGER DEFAULT 0;

ALTER TABLE parse_tasks ADD COLUMN IF NOT EXISTS structured_status VARCHAR(20) DEFAULT 'pending';
ALTER TABLE parse_tasks ADD COLUMN IF NOT EXISTS structured_progress INTEGER DEFAULT 0;
ALTER TABLE parse_tasks ADD COLUMN IF NOT EXISTS structured_element_count INTEGER;
ALTER TABLE parse_tasks ADD COLUMN IF NOT EXISTS structured_image_count INTEGER;
ALTER TABLE parse_tasks ADD COLUMN IF NOT EXISTS structured_table_count INTEGER;

ALTER TABLE parse_tasks ADD COLUMN IF NOT EXISTS ner_status VARCHAR(20) DEFAULT 'pending';
ALTER TABLE parse_tasks ADD COLUMN IF NOT EXISTS ner_progress INTEGER DEFAULT 0;
ALTER TABLE parse_tasks ADD COLUMN IF NOT EXISTS ner_task_id VARCHAR(64);
ALTER TABLE parse_tasks ADD COLUMN IF NOT EXISTS ner_entity_count INTEGER;
ALTER TABLE parse_tasks ADD COLUMN IF NOT EXISTS ner_relation_count INTEGER;

ALTER TABLE parse_tasks ADD COLUMN IF NOT EXISTS graph_status VARCHAR(20) DEFAULT 'pending';
ALTER TABLE parse_tasks ADD COLUMN IF NOT EXISTS graph_progress INTEGER DEFAULT 0;

-- 添加索引
CREATE INDEX IF NOT EXISTS idx_parse_tasks_ner_task_id ON parse_tasks(ner_task_id);

-- 添加注释
COMMENT ON COLUMN parse_tasks.parse_status IS '解析阶段状态: pending/processing/completed/failed';
COMMENT ON COLUMN parse_tasks.rag_status IS 'RAG向量化阶段状态';
COMMENT ON COLUMN parse_tasks.structured_status IS '结构化解析阶段状态';
COMMENT ON COLUMN parse_tasks.ner_status IS 'NER实体提取阶段状态';
COMMENT ON COLUMN parse_tasks.graph_status IS '图构建阶段状态';
COMMENT ON COLUMN parse_tasks.ner_task_id IS 'NER Python服务任务ID';
