from sqlalchemy import Column, Integer, String, Boolean, DateTime, ForeignKey, Text, Enum as SQLEnum
from sqlalchemy.orm import relationship
from database import Base
from datetime import datetime
import enum


class NotificationType(str, enum.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.Enum):
    LOW = "low"
    NORMAL = "normal"
    HIGH = "high"
    URGENT = "urgent"


class Notification(Base):
    __tablename__ = "notifications"

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False, index=True)
    type = Column(SQLEnum(NotificationType), nullable=False)
    priority = Column(SQLEnum(NotificationPriority), default=NotificationPriority.NORMAL)
    title = Column(String(255), nullable=False)
    message = Column(Text, nullable=False)
    read = Column(Boolean, default=False, index=True)
    
    # Optional references to related entities
    project_id = Column(Integer, ForeignKey("projects.id"), nullable=True)
    task_id = Column(Integer, ForeignKey("tasks.id"), nullable=True)
    submission_id = Column(Integer, ForeignKey("submissions.id"), nullable=True)
    
    # Email notification tracking
    email_sent = Column(Boolean, default=False)
    email_sent_at = Column(DateTime, nullable=True)
    
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    read_at = Column(DateTime, nullable=True)

    # Relationships
    user = relationship("User", back_populates="notifications")
    project = relationship("Project", foreign_keys=[project_id])
    task = relationship("Task", foreign_keys=[task_id])
    submission = relationship("Submission", foreign_keys=[submission_id])


class UserNotificationPreference(Base):
    __tablename__ = "user_notification_preferences"

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False, unique=True)
    
    # Email notification preferences
    email_enabled = Column(Boolean, default=True)
    email_task_assigned = Column(Boolean, default=True)
    email_task_status_changed = Column(Boolean, default=True)
    email_submission_reviewed = Column(Boolean, default=True)
    email_work_submitted = Column(Boolean, default=True)
    email_deadline_approaching = Column(Boolean, default=True)
    email_project_update = Column(Boolean, default=True)
    email_comment_added = Column(Boolean, default=True)
    
    # In-app notification preferences
    inapp_enabled = Column(Boolean, default=True)
    inapp_task_assigned = Column(Boolean, default=True)
    inapp_task_status_changed = Column(Boolean, default=True)
    inapp_submission_reviewed = Column(Boolean, default=True)
    inapp_work_submitted = Column(Boolean, default=True)
    inapp_deadline_approaching = Column(Boolean, default=True)
    inapp_project_update = Column(Boolean, default=True)
    inapp_comment_added = Column(Boolean, default=True)
    
    # Digest settings
    email_digest_enabled = Column(Boolean, default=False)
    email_digest_frequency = Column(String(50), default="daily")  # daily, weekly
    
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

    # Relationships
    user = relationship("User", back_populates="notification_preferences")
