# app/modules/messaging/schemas/notification.py
"""
Notification Pydantic schemas for API validation and responses.

This module provides schemas for:
- Store notifications (list, read, delete)
- Notification settings management
- Notification email templates
- Unread counts and statistics
"""

from datetime import datetime
from typing import Any

from pydantic import BaseModel, Field

# ============================================================================
# SHARED RESPONSE SCHEMAS
# ============================================================================


class MessageResponse(BaseModel):
    """Generic message response for simple operations."""

    message: str


class UnreadCountResponse(BaseModel):
    """Response for unread notification count."""

    unread_count: int
    message: str | None = None


# ============================================================================
# NOTIFICATION SCHEMAS
# ============================================================================


class NotificationResponse(BaseModel):
    """Single notification response."""

    id: int
    type: str
    title: str
    message: str
    is_read: bool
    read_at: datetime | None = None
    priority: str = "normal"
    action_url: str | None = None
    metadata: dict[str, Any] | None = None
    created_at: datetime

    model_config = {"from_attributes": True}


class NotificationListResponse(BaseModel):
    """Paginated list of notifications."""

    notifications: list[NotificationResponse] = []
    total: int = 0
    unread_count: int = 0
    message: str | None = None


# ============================================================================
# NOTIFICATION SETTINGS SCHEMAS
# ============================================================================


class NotificationSettingsResponse(BaseModel):
    """Notification preferences response."""

    email_notifications: bool = True
    in_app_notifications: bool = True
    notification_types: dict[str, bool] = Field(default_factory=dict)
    message: str | None = None


class NotificationSettingsUpdate(BaseModel):
    """Request model for updating notification settings."""

    email_notifications: bool | None = None
    in_app_notifications: bool | None = None
    notification_types: dict[str, bool] | None = None


# ============================================================================
# NOTIFICATION TEMPLATE SCHEMAS
# ============================================================================


class NotificationTemplateResponse(BaseModel):
    """Single notification template response."""

    id: int
    name: str
    type: str
    subject: str
    body_html: str | None = None
    body_text: str | None = None
    variables: list[str] = Field(default_factory=list)
    is_active: bool = True
    created_at: datetime
    updated_at: datetime | None = None

    model_config = {"from_attributes": True}


class NotificationTemplateListResponse(BaseModel):
    """List of notification templates."""

    templates: list[NotificationTemplateResponse] = []
    message: str | None = None


class NotificationTemplateUpdate(BaseModel):
    """Request model for updating notification template."""

    subject: str | None = Field(None, max_length=200)
    body_html: str | None = None
    body_text: str | None = None
    is_active: bool | None = None


# ============================================================================
# TEST NOTIFICATION SCHEMA
# ============================================================================


class TestNotificationRequest(BaseModel):
    """Request model for sending test notification."""

    template_id: int | None = Field(None, description="Template to use")
    email: str | None = Field(None, description="Override recipient email")
    notification_type: str = Field(
        default="test", description="Type of notification to send"
    )


# ============================================================================
# ADMIN ALERT STATISTICS SCHEMA
# ============================================================================


class AlertStatisticsResponse(BaseModel):
    """Response for alert statistics."""

    total_alerts: int = 0
    active_alerts: int = 0
    critical_alerts: int = 0
    resolved_today: int = 0
