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

class ReferralStatus(str, enum.Enum):
    pending = 'pending'
    completed = 'completed'
    rewarded = 'rewarded'

class Referral(Base):
    __tablename__ = 'referrals'
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    referrer_id = Column(Integer, ForeignKey('user_loyalty.id'), nullable=False, index=True)
    referred_user_id = Column(Integer, ForeignKey('users.id'), nullable=False, index=True)
    referral_code = Column(String(50), nullable=False, index=True)
    booking_id = Column(Integer, ForeignKey('bookings.id'), nullable=True, index=True)
    status = Column(Enum(ReferralStatus), nullable=False, default=ReferralStatus.pending, index=True)
    referrer_points_earned = Column(Integer, nullable=False, default=0)
    referred_points_earned = Column(Integer, nullable=False, default=0)
    completed_at = Column(DateTime, nullable=True)
    rewarded_at = Column(DateTime, nullable=True)
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False, index=True)
    
    referrer = relationship('UserLoyalty', foreign_keys=[referrer_id], back_populates='referrals')
    referred_user = relationship('User', foreign_keys=[referred_user_id])
    booking = relationship('Booking', foreign_keys=[booking_id])

