from __future__ import annotations

from typing import Dict

from celery import shared_task

from app.services.supabase_rest import supabase_rest


@shared_task(name="app.workers.tasks.ocr_pipeline")
def ocr_pipeline(task_id: str, document_id: str, file_url: str, user_id: str) -> Dict[str, str]:
    """
    阶段 0 Celery 任务：模拟 OCR，向 documents+background_tasks 回写占位结果。
    阶段 1 将在此处串联 MinerU OCR 与 LightRAG 索引。
    """
    supabase_rest.update("background_tasks", {"id": task_id}, {"status": "processing", "progress": 30})

    markdown = f"# OCR 结果占位\\n\\n文件地址：{file_url}\\n\\n> 阶段 1 将替换为 MinerU 输出。"
    supabase_rest.update(
        "documents",
        {"id": document_id, "user_id": user_id},
        {
            "content": {
                "blocks": [
                    {
                        "type": "paragraph",
                        "text": markdown,
                    }
                ]
            },
            "raw_text": markdown,
            "index_status": "completed",
        },
    )

    supabase_rest.update(
        "background_tasks",
        {"id": task_id},
        {
            "status": "completed",
            "progress": 100,
            "message": "OCR 模拟完成",
        },
    )

    return {
        "task_id": task_id,
        "document_id": document_id,
        "file_url": file_url,
        "status": "completed",
    }
