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 TransactionType(str, enum.Enum):
    earned = 'earned'
    redeemed = 'redeemed'
    expired = 'expired'
    bonus = 'bonus'
    adjustment = 'adjustment'

class TransactionSource(str, enum.Enum):
    booking = 'booking'
    referral = 'referral'
    birthday = 'birthday'
    anniversary = 'anniversary'
    redemption = 'redemption'
    promotion = 'promotion'
    manual = 'manual'

class LoyaltyPointTransaction(Base):
    __tablename__ = 'loyalty_point_transactions'
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    user_loyalty_id = Column(Integer, ForeignKey('user_loyalty.id'), nullable=False, index=True)
    booking_id = Column(Integer, ForeignKey('bookings.id'), nullable=True, index=True)
    transaction_type = Column(Enum(TransactionType), nullable=False, index=True)
    source = Column(Enum(TransactionSource), nullable=False)
    points = Column(Integer, nullable=False)
    description = Column(Text, nullable=True)
    expires_at = Column(DateTime, nullable=True)
    reference_number = Column(String(100), nullable=True)
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False, index=True)
    
    # Relationships
    user_loyalty = relationship('UserLoyalty', back_populates='point_transactions')
    booking = relationship('Booking', foreign_keys=[booking_id])

