from sqlalchemy import Column, Integer, String, Boolean, DateTime, ForeignKey, Text
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from ..database import Base


class PushSubscription(Base):
    """Store user's push notification subscriptions"""
    __tablename__ = "push_subscriptions"

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    endpoint = Column(Text, nullable=False)  # Push service endpoint URL
    p256dh_key = Column(String(255), nullable=False)  # Public key for encryption
    auth_key = Column(String(255), nullable=False)  # Auth secret for encryption
    device_info = Column(String(255), nullable=True)  # Browser/device info
    is_active = Column(Boolean, default=True)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    last_used_at = Column(DateTime(timezone=True), nullable=True)

    user = relationship("User", backref="push_subscriptions")


class UserNotificationPreference(Base):
    """User preferences for different notification types"""
    __tablename__ = "user_notification_preferences"

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False, unique=True)

    # Notification type preferences (True = enabled)
    vehicle_recommended = Column(Boolean, default=True)
    shipping_update = Column(Boolean, default=True)
    payment_confirmed = Column(Boolean, default=True)
    withdrawal_processed = Column(Boolean, default=True)
    dealer_status = Column(Boolean, default=True)
    share_purchased = Column(Boolean, default=True)
    referral_reward = Column(Boolean, default=True)
    inquiry_reply = Column(Boolean, default=True)
    system_announcements = Column(Boolean, default=True)

    # Channel preferences
    push_enabled = Column(Boolean, default=True)
    email_enabled = Column(Boolean, default=False)

    updated_at = Column(DateTime(timezone=True), onupdate=func.now())

    user = relationship("User", backref="notification_preferences")
