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

class TierLevel(str, enum.Enum):
    bronze = 'bronze'
    silver = 'silver'
    gold = 'gold'
    platinum = 'platinum'

class LoyaltyTier(Base):
    __tablename__ = 'loyalty_tiers'
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    level = Column(Enum(TierLevel), unique=True, nullable=False, index=True)
    name = Column(String(100), nullable=False)
    description = Column(Text, nullable=True)
    min_points = Column(Integer, nullable=False, default=0)
    points_earn_rate = Column(Numeric(5, 2), nullable=False, default=1.0)
    discount_percentage = Column(Numeric(5, 2), nullable=True, default=0)
    benefits = Column(Text, nullable=True)
    icon = Column(String(255), nullable=True)
    color = Column(String(50), nullable=True)
    is_active = Column(Boolean, nullable=False, default=True)
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
    
    # Relationships
    user_tiers = relationship('UserLoyalty', back_populates='tier')

