"""
Nudge Schema

Proactive suggestions/prompts generated by the Mind.
"""

from datetime import datetime
from typing import Literal

from pydantic import BaseModel, Field


class Nudge(BaseModel):
    """
    A proactive nudge/suggestion for the user.

    Nudges are generated based on patterns, inactivity, or
    opportunities detected by the Mind.
    """

    id: str = Field(default="", description="Unique identifier")
    project_mind_id: str = Field(..., description="Reference to ProjectMind")

    # Nudge content
    nudge_type: str = Field(..., description="Type of nudge (from adapter templates)")
    title: str = Field(..., description="Short headline")
    message: str = Field(..., description="Full nudge message")

    # Priority and timing
    priority: int = Field(
        default=5,
        ge=1,
        le=10,
        description="Priority 1-10 (higher = more important)",
    )
    scheduled_for: datetime | None = Field(
        default=None,
        description="When to deliver (None = immediate)",
    )

    # State
    status: Literal["pending", "delivered", "dismissed", "acted"] = Field(
        default="pending",
        description="Nudge status",
    )
    delivered_at: datetime | None = Field(default=None)
    dismissed_at: datetime | None = Field(default=None)
    acted_at: datetime | None = Field(default=None)

    # Context
    context: dict = Field(
        default_factory=dict,
        description="Additional context for the nudge",
    )

    # Timestamps
    created_at: datetime = Field(default_factory=datetime.utcnow)
    updated_at: datetime = Field(default_factory=datetime.utcnow)

    model_config = {"extra": "allow"}

    def deliver(self) -> None:
        """Mark nudge as delivered."""
        self.status = "delivered"
        self.delivered_at = datetime.utcnow()
        self.updated_at = datetime.utcnow()

    def dismiss(self) -> None:
        """Mark nudge as dismissed."""
        self.status = "dismissed"
        self.dismissed_at = datetime.utcnow()
        self.updated_at = datetime.utcnow()

    def mark_acted(self) -> None:
        """Mark nudge as acted upon."""
        self.status = "acted"
        self.acted_at = datetime.utcnow()
        self.updated_at = datetime.utcnow()
