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

class PackageStatus(str, enum.Enum):
    active = 'active'
    inactive = 'inactive'
    scheduled = 'scheduled'
    expired = 'expired'

class PackageItemType(str, enum.Enum):
    room = 'room'
    service = 'service'
    breakfast = 'breakfast'
    activity = 'activity'
    amenity = 'amenity'
    discount = 'discount'

class Package(Base):
    __tablename__ = 'packages'
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    name = Column(String(100), nullable=False, index=True)
    code = Column(String(50), unique=True, nullable=False, index=True)
    description = Column(Text, nullable=True)
    status = Column(Enum(PackageStatus), nullable=False, default=PackageStatus.active)
    
    # Pricing
    base_price = Column(Numeric(10, 2), nullable=True)  # Fixed package price (if set, overrides item prices)
    price_modifier = Column(Numeric(5, 2), nullable=False, default=1.0)  # Multiplier for total package price
    discount_percentage = Column(Numeric(5, 2), nullable=True, default=0)
    
    # Applicability
    room_type_id = Column(Integer, ForeignKey('room_types.id'), nullable=True)  # None = all room types
    min_nights = Column(Integer, nullable=True)
    max_nights = Column(Integer, nullable=True)
    
    # Date range
    valid_from = Column(Date, nullable=True)
    valid_to = Column(Date, nullable=True)
    
    # Package details
    image_url = Column(String(500), nullable=True)
    highlights = Column(JSON, nullable=True)  # Array of highlight strings
    terms_conditions = Column(Text, nullable=True)
    
    # Additional data (metadata is reserved by SQLAlchemy)
    extra_data = Column(JSON, nullable=True)
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
    
    # Relationships
    room_type = relationship('RoomType', back_populates='packages')
    items = relationship('PackageItem', back_populates='package', cascade='all, delete-orphan')
    rate_plans = relationship('RatePlan', back_populates='package')

class PackageItem(Base):
    __tablename__ = 'package_items'
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    package_id = Column(Integer, ForeignKey('packages.id'), nullable=False, index=True)
    
    # Item details
    item_type = Column(Enum(PackageItemType), nullable=False)
    item_id = Column(Integer, nullable=True)  # ID of room_type, service, etc.
    item_name = Column(String(200), nullable=False)  # Name for display
    item_description = Column(Text, nullable=True)
    
    # Quantity
    quantity = Column(Integer, nullable=False, default=1)
    unit = Column(String(50), nullable=True)  # 'per_night', 'per_stay', 'per_person', etc.
    
    # Pricing
    price = Column(Numeric(10, 2), nullable=True)  # Item price (if set)
    included = Column(Boolean, nullable=False, default=True)  # If True, included in package price
    price_modifier = Column(Numeric(5, 2), nullable=True, default=1.0)  # Price multiplier for this item
    
    # Display order
    display_order = Column(Integer, nullable=False, default=0)
    
    # Additional data
    extra_data = Column(JSON, nullable=True)
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
    
    # Relationships
    package = relationship('Package', back_populates='items')

