from __future__ import annotations

from dataclasses import dataclass, field
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from app.schemas.agent_config import AgentConfig

import httpx
from sqlalchemy.ext.asyncio import AsyncSession


@dataclass
class CallerIdentity:
    """Who is calling the agent and how trustworthy that identity is.

    Constructed by chat entry points (dashboard, website, WhatsApp) before
    agent execution begins.  Tools read this via ``ctx.deps.caller`` to
    enforce authorization.
    """

    channel: str  # 'dashboard' | 'website' | 'whatsapp' | 'instagram' | 'telegram'
    # Better Auth user id (dashboard channel), conversation identity key, or phone number
    identity_key: str
    customer_id: str | None = None
    customer_name: str | None = None
    customer_email: str | None = None
    verified: bool = False
    conversation_id: str | None = None


@dataclass
class AgentDeps:
    session: AsyncSession
    restaurant_id: str
    http_client: httpx.AsyncClient
    caller: CallerIdentity  # required — no default, construction without caller must fail
    language: str = "nl"
    timezone: str = "Europe/Brussels"
    enabled_capabilities: set[str] = field(default_factory=lambda: {"faq"})
    agent_config: AgentConfig = field(default_factory=lambda: _default_agent_config())


def _default_agent_config() -> AgentConfig:
    from app.schemas.agent_config import AgentConfig

    return AgentConfig()
