# loom/receipts/vision_receipt.py

from __future__ import annotations

from datetime import datetime, timezone
from typing import Optional, Dict, Any, List, Literal

from pydantic import BaseModel, Field
import uuid


class VisionReceipt(BaseModel):
    """
    Observation of visual state (screen or camera), with OCR and meta.

    v0: screen-only, text-oriented.
    """

    type: Literal["VisionReceipt"] = "VisionReceipt"

    receipt_id: str
    created_at: datetime

    observer_id: str = "ScreenObserver"

    source: str  # "screen" | "camera" | "file"
    ocr_text: str = ""
    detected_objects: List[str] = Field(default_factory=list)
    caption: Optional[str] = None

    meta: Dict[str, Any] = Field(default_factory=dict)

    # Governance / Profile Metadata
    profile: str = "enterprise"
    receipt_mode: str = "minimal"
    retention_policy: str = "30d"
    encryption_mode: str = "none"
    consent_token: Optional[str] = None

    class Config:
        use_enum_values = True


def build_vision_receipt(
    *,
    source: str,
    ocr_text: str,
    observer_id: str = "ScreenObserver",
    detected_objects: Optional[list[str]] = None,
    caption: Optional[str] = None,
    meta: Optional[dict] = None,
) -> VisionReceipt:
    now = datetime.now(timezone.utc)
    rid = f"vis_{now.timestamp():.3f}_{uuid.uuid4().hex[:8]}"

    return VisionReceipt(
        receipt_id=rid,
        created_at=now,
        observer_id=observer_id,
        source=source,
        ocr_text=ocr_text,
        detected_objects=detected_objects or [],
        caption=caption,
        meta=meta or {},
    )
