from __future__ import annotations

from sqlalchemy.ext.asyncio import AsyncSession

from app.models.chat_generation_task import ChatGenerationTask


async def notify_chat_generation_task_finished(db: AsyncSession, task: ChatGenerationTask) -> None:
    """通知业务模块 ChatGenerationTask 已进入终态。

    当前用于爆款开头复刻：
    - image_generate 完成后自动进入 video_prompt_optimize
    - video_generate 完成后总任务完成
    """
    if not task:
        return
    if task.generation_mode == "hot_opening_replicate":
        from app.services.hot_opening_replicate_service import (
            handle_chat_generation_task_completed,
            handle_chat_generation_task_failed,
        )
        if task.status == "completed":
            await handle_chat_generation_task_completed(db, task)
        elif task.status == "failed":
            await handle_chat_generation_task_failed(db, task)
        return

    if task.generation_mode == "shot_replicate":
        from app.services.shot_replicate_flow_service import (
            handle_chat_generation_task_completed,
            handle_chat_generation_task_failed,
        )
        if task.status == "completed":
            await handle_chat_generation_task_completed(db, task)
        elif task.status == "failed":
            await handle_chat_generation_task_failed(db, task)
        return
