from typing import TypedDict, Optional, Any
import uuid
from datetime import datetime


class EngineeringState(TypedDict):
    # === Identity ===
    task_id: str
    project_id: Optional[str]
    created_at: str

    # === User Input ===
    user_request: str
    repo_path: str

    # === Runtime ===
    sandbox_path: str
    selected_executor: str
    attempts: int
    max_attempts: int
    status: str  # pending | running | success | failed | reviewing | fixed

    # === Agent Outputs ===
    project_analysis: dict        # from review_project / supervisor
    architecture_plan: dict       # from architect
    implementation_plan: list     # task list from supervisor
    current_task: Optional[str]   # current sub-task being executed

    # === Results ===
    code_result: dict             # stdout, stderr, files_changed
    test_result: dict             # passed, failed, coverage, output
    review_result: dict           # bugs, security, performance, suggestions
    diff_result: dict             # git diff summary

    # === Output ===
    report: str
    pr_description: str
    error: Optional[str]

    # === Memory ===
    memory_context: Optional[dict]


def create_initial_state(
    user_request: str,
    repo_path: str,
    executor: str = "claude",
    project_id: Optional[str] = None,
) -> EngineeringState:
    return EngineeringState(
        task_id=str(uuid.uuid4()),
        project_id=project_id,
        created_at=datetime.utcnow().isoformat(),
        user_request=user_request,
        repo_path=repo_path,
        sandbox_path="",
        selected_executor=executor,
        attempts=0,
        max_attempts=3,
        status="pending",
        project_analysis={},
        architecture_plan={},
        implementation_plan=[],
        current_task=None,
        code_result={},
        test_result={},
        review_result={},
        diff_result={},
        report="",
        pr_description="",
        error=None,
        memory_context=None,
    )
