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 RedemptionStatus(str, enum.Enum):
    pending = 'pending'
    active = 'active'
    used = 'used'
    expired = 'expired'
    cancelled = 'cancelled'

class RewardRedemption(Base):
    __tablename__ = 'reward_redemptions'
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    user_loyalty_id = Column(Integer, ForeignKey('user_loyalty.id'), nullable=False, index=True)
    reward_id = Column(Integer, ForeignKey('loyalty_rewards.id'), nullable=False, index=True)
    booking_id = Column(Integer, ForeignKey('bookings.id'), nullable=True, index=True)
    points_used = Column(Integer, nullable=False)
    status = Column(Enum(RedemptionStatus), nullable=False, default=RedemptionStatus.pending, index=True)
    code = Column(String(50), unique=True, nullable=True, index=True)
    expires_at = Column(DateTime, nullable=True)
    used_at = Column(DateTime, nullable=True)
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
    
    # Relationships
    user_loyalty = relationship('UserLoyalty', foreign_keys=[user_loyalty_id])
    reward = relationship('LoyaltyReward', back_populates='redemptions')
    booking = relationship('Booking', foreign_keys=[booking_id])

