"""Pydantic data models for the multi-agent meeting framework."""

from __future__ import annotations

from datetime import datetime
from typing import Literal, Optional

from pydantic import BaseModel, model_validator


class AgentConfig(BaseModel):
    """Configuration for a single Agent."""

    name: str
    role: str
    system_prompt: str
    input_schema: str = ""
    output_schema: str = ""


class ScratchpadEntry(BaseModel):
    """A single entry on the shared whiteboard."""

    agent_name: str
    content: str
    timestamp: datetime


class MeetingState(BaseModel):
    """The shared whiteboard maintaining all meeting context."""

    topic: str
    context: str = ""
    context_summary: str = ""
    scratchpad: list[ScratchpadEntry] = []
    current_round: int = 0
    max_rounds: int = 10


class PMDecision(BaseModel):
    """Structured output from the PM Agent each round."""

    analysis: str
    next_action: Literal["CALL_AGENT", "FINISH"]
    target_agent: Optional[str] = None
    prompt_for_agent: Optional[str] = None
    final_report: Optional[str] = None

    @model_validator(mode="after")
    def validate_fields_for_action(self) -> PMDecision:
        """Ensure required fields are present based on next_action."""
        if self.next_action == "CALL_AGENT":
            if not self.target_agent:
                raise ValueError("target_agent is required when next_action is CALL_AGENT")
            if not self.prompt_for_agent:
                raise ValueError("prompt_for_agent is required when next_action is CALL_AGENT")
        elif self.next_action == "FINISH":
            if not self.final_report:
                raise ValueError("final_report is required when next_action is FINISH")
        return self


class MeetingResult(BaseModel):
    """Complete result of a meeting including transcript and metadata."""

    topic: str
    context: str = ""
    participants: list[str] = []
    transcript: list[ScratchpadEntry] = []
    final_report: str
    started_at: datetime
    finished_at: datetime
