from pydantic import BaseModel, Field
from datetime import datetime
from typing import Optional
from enum import Enum


class NotificationType(str, Enum):
    TASK_ASSIGNED = "task_assigned"
    TASK_STATUS_CHANGED = "task_status_changed"
    SUBMISSION_REVIEWED = "submission_reviewed"
    WORK_SUBMITTED = "work_submitted"
    DEADLINE_APPROACHING = "deadline_approaching"
    PROJECT_UPDATE = "project_update"
    COMMENT_ADDED = "comment_added"


class NotificationPriority(str, Enum):
    LOW = "low"
    NORMAL = "normal"
    HIGH = "high"
    URGENT = "urgent"


class NotificationBase(BaseModel):
    type: NotificationType
    priority: NotificationPriority = NotificationPriority.NORMAL
    title: str = Field(..., max_length=255)
    message: str
    project_id: Optional[int] = None
    task_id: Optional[int] = None
    submission_id: Optional[int] = None


class NotificationCreate(NotificationBase):
    user_id: int


class NotificationResponse(NotificationBase):
    id: int
    user_id: int
    read: bool
    email_sent: bool
    email_sent_at: Optional[datetime] = None
    created_at: datetime
    read_at: Optional[datetime] = None

    class Config:
        from_attributes = True


class NotificationMarkRead(BaseModel):
    notification_ids: list[int]


class NotificationPreferencesBase(BaseModel):
    # Email notification preferences
    email_enabled: bool = True
    email_task_assigned: bool = True
    email_task_status_changed: bool = True
    email_submission_reviewed: bool = True
    email_work_submitted: bool = True
    email_deadline_approaching: bool = True
    email_project_update: bool = True
    email_comment_added: bool = True
    
    # In-app notification preferences
    inapp_enabled: bool = True
    inapp_task_assigned: bool = True
    inapp_task_status_changed: bool = True
    inapp_submission_reviewed: bool = True
    inapp_work_submitted: bool = True
    inapp_deadline_approaching: bool = True
    inapp_project_update: bool = True
    inapp_comment_added: bool = True
    
    # Digest settings
    email_digest_enabled: bool = False
    email_digest_frequency: str = "daily"


class NotificationPreferencesUpdate(NotificationPreferencesBase):
    pass


class NotificationPreferencesResponse(NotificationPreferencesBase):
    id: int
    user_id: int
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class NotificationStats(BaseModel):
    total: int
    unread: int
    by_type: dict[str, int]
