"""
Pydantic schemas for the Notifications module.
"""

from datetime import datetime
from typing import Any, Dict, List, Optional
from pydantic import BaseModel, Field


class NotificationTemplateSchema(BaseModel):
    """Read schema for a notification template."""

    id: int
    merchant_id: Optional[int] = None
    template_key: str
    channel: str
    subject: Optional[str] = None
    body_html: Optional[str] = None
    body_text: Optional[str] = None
    is_active: bool = True
    created_at: Optional[datetime] = None
    updated_at: Optional[datetime] = None

    model_config = {"from_attributes": True}


class SendNotificationSchema(BaseModel):
    """Internal schema used when services request a notification send."""

    template_key: str = Field(..., description="Template identifier")
    channel: str = Field(..., description="email | sms")
    recipient_email: Optional[str] = Field(None, description="Recipient email for email channel")
    recipient_phone: Optional[str] = Field(None, description="Recipient phone for sms channel")
    template_vars: Dict[str, Any] = Field(
        default_factory=dict,
        description="Variables to substitute into the template body",
    )
    merchant_id: Optional[int] = Field(None, description="Used to resolve merchant-specific template override")


class NotificationResultSchema(BaseModel):
    """Result of a notification send attempt."""

    success: bool
    channel: str
    recipient: str
    template_key: str
    error: Optional[str] = None
    message: str = "Notification dispatched"
