from __future__ import annotations

import uuid
from typing import Any, Dict, List, Literal, Optional

from pydantic import Field

from receipts.base import BaseReceipt


class ValueDemandReceipt(BaseReceipt):
    """
    Demand to turn an unresolved insight into action.
    Emitted by the Questioner organ when Collapse Debt is detected.
    """

    receipt_type: Literal["ValueDemandReceipt"] = Field(default="ValueDemandReceipt")
    domain: Literal["value"] = Field(default="value")
    organ: Literal["questioner"] = Field(default="questioner")

    demand_id: str = Field(
        default_factory=lambda: f"VDR_{uuid.uuid4().hex}",
        description="Unique identifier for this demand (stable across retries).",
    )
    source_receipt_ids: List[str] = Field(
        default_factory=list,
        description="Upstream receipts that motivated this demand.",
    )
    insight_summary: str = Field(
        default="",
        description="Short summary of the unresolved insight.",
    )
    proposed_action: str = Field(
        default="",
        description="One concrete, low-risk next step to realize value.",
    )
    expected_delta_v: str = Field(
        default="",
        description="Why this matters; expected value change.",
    )
    crif_snapshot: Optional[Dict[str, float]] = Field(
        default=None,
        description="Optional CRIF signature captured when demand was minted.",
    )
    delta_c: Optional[float] = Field(
        default=None,
        description="Optional coherence delta for the motivating insight.",
    )
    outcome_receipt_ids: List[str] = Field(
        default_factory=list,
        description="Linked outcomes that satisfy this demand.",
    )
    value_resolved: bool = Field(
        default=False,
        description="True once an outcome receipt is linked or demand is fulfilled.",
    )
    status: Literal["open", "accepted", "completed"] = Field(
        default="open",
        description="Lifecycle status of the demand.",
    )


class OutcomeReceipt(BaseReceipt):
    """
    Outcome emitted when a ValueDemandReceipt has been satisfied.
    """

    receipt_type: Literal["OutcomeReceipt"] = Field(default="OutcomeReceipt")
    domain: Literal["value"] = Field(default="value")
    organ: Literal["questioner"] = Field(default="questioner")

    outcome_id: str = Field(
        default_factory=lambda: f"OUT_{uuid.uuid4().hex}",
        description="Unique identifier for this outcome.",
    )
    demand_id: Optional[str] = Field(
        default=None,
        description="Demand this outcome satisfies.",
    )
    summary: str = Field(
        default="",
        description="What was done or shipped.",
    )
    evidence: Dict[str, Any] = Field(
        default_factory=dict,
        description="Proof artifacts or metrics collected.",
    )
    value_realized: Optional[str] = Field(
        default=None,
        description="Optional narrative of the realized ΔV.",
    )
    crif_snapshot: Optional[Dict[str, float]] = Field(
        default=None,
        description="Optional CRIF signature at completion time.",
    )

