"""BloomOS Task Request/Response Schemas"""

from pydantic import BaseModel, Field
from typing import Optional, List, Dict, Any


class TaskRequest(BaseModel):
    """
    Task request sent to BloomOS runtime.

    Fields:
        task_type: Category of task (math, code, json, search, reason, etc.)
        description: Natural language description or KRISPER/BioPoetica source
        anchors: List of anchor names to use (default: inferred from task_type)
        gate_policy: Override gate selection (default: auto from task_type)
        helix_program: Full 8-codon program override (e.g., "BGGDDGEA" for math)
                      Can be a named program like "MATH64_ALPHA" or raw sequence
        metadata: Optional metadata (user_id, session_id, etc.)
    """
    task_type: str = Field(..., description="Task category: math, code, json, search, reason")
    description: str = Field(..., description="Natural language task description")
    anchors: Optional[List[str]] = Field(None, description="Anchor names (default: auto)")
    gate_policy: Optional[str] = Field(None, description="Gate policy override: math_bias, code_bias, json_bias")
    helix_program: Optional[str] = Field(None, description="Full 8-codon program or named program (MATH64_ALPHA, CODE64_ALPHA, etc.)")
    metadata: Optional[Dict[str, Any]] = Field(default_factory=dict, description="Optional metadata")


class TaskResult(BaseModel):
    """
    Task result returned from BloomOS runtime.

    Fields:
        task_id: Unique task identifier (SHA256 hash of request + timestamp)
        status: ok, error, vm_error
        output: Result data (structure depends on task_type)
        gate_used: Which gate policy was applied
        helix_program_used: Full 8-codon program that was executed
        anchors_used: List of anchors that were used
        logits_mean: Mean logits from Helix64 execution
        logits_std: Std deviation of logits
        receipt: Receipt data for provenance (optional)
        error: Error message if status != ok
    """
    task_id: str = Field(..., description="Unique task ID (SHA256-based)")
    status: str = Field(..., description="Task status: ok, error, vm_error")
    output: Optional[Any] = Field(None, description="Task output data")
    gate_used: str = Field(..., description="Gate policy that was applied")
    helix_program_used: Optional[str] = Field(None, description="Full 8-codon program executed")
    anchors_used: List[str] = Field(..., description="Anchors used in execution")
    logits_mean: Optional[float] = Field(None, description="Mean logits from Helix64")
    logits_std: Optional[float] = Field(None, description="Logits std deviation")
    receipt: Optional[Dict[str, Any]] = Field(None, description="Receipt for provenance")
    error: Optional[str] = Field(None, description="Error message if status != ok")
