from __future__ import annotations

import json

from app.core.agent.processes.v2.workflows.doc_update_from_feature_v2.workflow_runtime.models import RequirementTaskContext
from app.core.agent.utils.llm import AgentLlmService


class EditDetailsStep:
    def __init__(self, llm: AgentLlmService) -> None:
        self._llm = llm

    def run(
        self,
        task: RequirementTaskContext,
        current_content: str,
        rules_text: str,
        shared_context: dict[str, object],
    ) -> str:
        payload = {
            "task": {
                "section_key": task.section_key,
                "doc_type": task.doc_type,
                "id": task.doc_id,
                "heading": task.heading,
                "domain": task.domain,
                "sub_domain": task.subdomain,
                "path": task.path,
            },
            "current_content": current_content,
            "requirement_text": task.body,
            "rules": rules_text,
            "shared_context": shared_context,
        }
        return self._llm.generate(
            "v2_docs_update_v2.edit_details",
            json.dumps(payload, ensure_ascii=False, indent=2),
            log_context="workflow.v2.docs_update.from_feature_v2.edit.details",
        ).strip()
