from sqlalchemy import Column, Integer, String, Boolean, Text, ForeignKey, DateTime, Numeric
from sqlalchemy.orm import relationship
from datetime import datetime
from ...shared.config.database import Base

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    role_id = Column(Integer, ForeignKey('roles.id'), nullable=False)
    email = Column(String(100), unique=True, nullable=False, index=True)
    password = Column(String(255), nullable=False)
    full_name = Column(String(100), nullable=False)
    phone = Column(String(20), nullable=True)
    address = Column(Text, nullable=True)
    avatar = Column(String(255), nullable=True)
    currency = Column(String(3), nullable=False, default='VND')
    is_active = Column(Boolean, nullable=False, default=True)
    email_verified = Column(Boolean, nullable=False, default=False, index=True)  # Email verification status
    mfa_enabled = Column(Boolean, nullable=False, default=False)
    mfa_secret = Column(String(255), nullable=True)
    mfa_backup_codes = Column(Text, nullable=True)
    
    # Account lockout fields
    failed_login_attempts = Column(Integer, nullable=False, default=0)
    locked_until = Column(DateTime, nullable=True)
    
    # Password expiry (optional - can be None for no expiry)
    password_changed_at = Column(DateTime, nullable=True, index=True)
    
    # Guest Profile & CRM fields
    is_vip = Column(Boolean, nullable=False, default=False)
    lifetime_value = Column(Numeric(10, 2), nullable=True, default=0)  # Total revenue from guest
    satisfaction_score = Column(Numeric(3, 2), nullable=True)  # Average satisfaction score (0-5)
    last_visit_date = Column(DateTime, nullable=True)  # Last booking check-in date
    total_visits = Column(Integer, nullable=False, default=0)  # Total number of bookings
    
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
    role = relationship('Role', back_populates='users')
    bookings = relationship('Booking', back_populates='user')
    refresh_tokens = relationship('RefreshToken', back_populates='user', cascade='all, delete-orphan')
    checkins_processed = relationship('CheckInCheckOut', foreign_keys='CheckInCheckOut.checkin_by', back_populates='checked_in_by')
    checkouts_processed = relationship('CheckInCheckOut', foreign_keys='CheckInCheckOut.checkout_by', back_populates='checked_out_by')
    reviews = relationship('Review', back_populates='user')
    favorites = relationship('Favorite', back_populates='user', cascade='all, delete-orphan')
    service_bookings = relationship('ServiceBooking', back_populates='user')
    visitor_chats = relationship('Chat', foreign_keys='Chat.visitor_id', back_populates='visitor')
    staff_chats = relationship('Chat', foreign_keys='Chat.staff_id', back_populates='staff')
    loyalty = relationship('UserLoyalty', back_populates='user', uselist=False, cascade='all, delete-orphan')
    referrals = relationship('Referral', foreign_keys='Referral.referred_user_id', back_populates='referred_user')
    
    # Guest Profile & CRM relationships
    guest_preferences = relationship('GuestPreference', back_populates='user', uselist=False, cascade='all, delete-orphan')
    guest_notes = relationship('GuestNote', foreign_keys='GuestNote.user_id', back_populates='user', cascade='all, delete-orphan')
    guest_tags = relationship('GuestTag', secondary='guest_tag_associations', back_populates='users')
    guest_communications = relationship('GuestCommunication', foreign_keys='GuestCommunication.user_id', back_populates='user', cascade='all, delete-orphan')
    guest_segments = relationship('GuestSegment', secondary='guest_segment_associations', back_populates='users')
    email_verification_tokens = relationship('EmailVerificationToken', back_populates='user', cascade='all, delete-orphan')
    password_history = relationship('PasswordHistory', back_populates='user', cascade='all, delete-orphan', order_by='PasswordHistory.created_at.desc()')