from typing import TYPE_CHECKING, List

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


class User(Base):
    """User model for customer accounts"""
    __tablename__ = "users"
    
    user_id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    email = Column(String(255), unique=True, index=True, nullable=False)
    hashed_password = Column(String(255), nullable=False)
    full_name = Column(String(255), nullable=True)
    phone_number = Column(String(20), nullable=True)
    address = Column(String(500), nullable=True)
    profile_image = Column(String(500), nullable=True)
    latitude = Column(Float, nullable=True)
    longitude = Column(Float, nullable=True)
    timezone = Column(String(100), nullable=True)
    is_verified = Column(Boolean, default=False, nullable=False)
    is_active = Column(Boolean, default=True, nullable=False)
    is_travel_enabled = Column(Boolean, default=False, nullable=False)
    enable_notification = Column(Boolean, default=True, nullable=False)  # Enable/disable push notifications
    device_token = Column(String(500), nullable=True)  # FCM device token for push notifications
    is_pro = Column(Boolean, default=False, nullable=False)  # Subscription status: True for Pro, False for Free
    subscription_expires_at = Column(DateTime(timezone=True), nullable=True)  # When Pro subscription expires
    created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False)

    if TYPE_CHECKING:
        from app.models.user.order_model import UserOrder  # pragma: no cover
        from app.models.user.favorite_model import UserFavorite  # pragma: no cover
        from app.models.user.notification_model import UserNotification  # pragma: no cover
        from app.models.user.subscription_model import UserSubscription  # pragma: no cover

    orders: Mapped[List["UserOrder"]] = relationship("UserOrder", back_populates="user", cascade="all, delete-orphan")
    favorites: Mapped[List["UserFavorite"]] = relationship("UserFavorite", back_populates="user", cascade="all, delete-orphan")
    notifications: Mapped[List["UserNotification"]] = relationship("UserNotification", back_populates="user", cascade="all, delete-orphan")
    subscriptions: Mapped[List["UserSubscription"]] = relationship("UserSubscription", back_populates="user", cascade="all, delete-orphan")


class OTPVerification(Base):
    """OTP verification model for email verification"""
    __tablename__ = "otp_verifications"
    
    otp_id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    email = Column(String(255), index=True, nullable=False)
    otp_code = Column(String(6), nullable=False)
    is_used = Column(Boolean, default=False, nullable=False)
    expires_at = Column(DateTime(timezone=True), nullable=False)
    created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)


class TokenBlacklist(Base):
    """Token blacklist for revoked refresh tokens"""
    __tablename__ = "token_blacklist"
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    token = Column(Text, unique=True, index=True, nullable=False)
    user_id = Column(Integer, nullable=False)
    blacklisted_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
    expires_at = Column(DateTime(timezone=True), nullable=False)
    reason = Column(String(100), nullable=True)  # e.g., "refresh", "logout", "security"


class UserAddress(Base):
    """User addresses with support for multiple entries and a single active address"""
    __tablename__ = "user_addresses"

    address_id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    user_id = Column(Integer, ForeignKey("users.user_id", ondelete="CASCADE"), nullable=False, index=True)
    label = Column(String(100), nullable=True)
    address_line1 = Column(String(255), nullable=False)
    address_line2 = Column(String(255), nullable=True)
    city = Column(String(100), nullable=True)
    state = Column(String(100), nullable=True)
    postal_code = Column(String(20), nullable=True)
    country = Column(String(100), nullable=True)
    latitude = Column(Float, nullable=True)
    longitude = Column(Float, nullable=True)
    is_active = Column(Boolean, default=False, nullable=False)
    created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False)

    if TYPE_CHECKING:
        pass  # pragma: no cover

