from sqlalchemy import Column, Integer, String, Boolean, Text, ForeignKey, DateTime
from sqlalchemy.orm import relationship
from datetime import datetime
from ..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)
    mfa_enabled = Column(Boolean, nullable=False, default=False)
    mfa_secret = Column(String(255), nullable=True)
    mfa_backup_codes = Column(Text, nullable=True)
    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')