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

class InventoryCategory(str, enum.Enum):
    cleaning_supplies = 'cleaning_supplies'
    linens = 'linens'
    toiletries = 'toiletries'
    amenities = 'amenities'
    maintenance = 'maintenance'
    food_beverage = 'food_beverage'
    other = 'other'

class InventoryUnit(str, enum.Enum):
    piece = 'piece'
    box = 'box'
    bottle = 'bottle'
    roll = 'roll'
    pack = 'pack'
    liter = 'liter'
    kilogram = 'kilogram'
    meter = 'meter'
    other = 'other'

class InventoryItem(Base):
    __tablename__ = 'inventory_items'
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    name = Column(String(255), nullable=False, index=True)
    description = Column(Text, nullable=True)
    category = Column(Enum(InventoryCategory), nullable=False, default=InventoryCategory.other)
    unit = Column(Enum(InventoryUnit), nullable=False, default=InventoryUnit.piece)
    
    # Stock tracking
    current_quantity = Column(Numeric(10, 2), nullable=False, default=0)
    minimum_quantity = Column(Numeric(10, 2), nullable=False, default=0)  # Reorder threshold
    maximum_quantity = Column(Numeric(10, 2), nullable=True)  # Max stock level
    reorder_quantity = Column(Numeric(10, 2), nullable=True)  # Suggested reorder amount
    
    # Pricing
    unit_cost = Column(Numeric(10, 2), nullable=True)
    supplier = Column(String(255), nullable=True)
    supplier_contact = Column(Text, nullable=True)
    
    # Location
    storage_location = Column(String(255), nullable=True)  # e.g., "Housekeeping Storage Room A"
    
    # Status
    is_active = Column(Boolean, nullable=False, default=True)
    is_tracked = Column(Boolean, nullable=False, default=True)  # Whether to track consumption
    
    # Metadata
    barcode = Column(String(100), nullable=True, unique=True, index=True)
    sku = Column(String(100), nullable=True, unique=True, index=True)
    notes = Column(Text, nullable=True)
    
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
    created_by = Column(Integer, ForeignKey('users.id'), nullable=True)
    
    # Relationships
    transactions = relationship('InventoryTransaction', back_populates='item', cascade='all, delete-orphan')
    reorder_requests = relationship('InventoryReorderRequest', back_populates='item', cascade='all, delete-orphan')
    task_consumptions = relationship('InventoryTaskConsumption', back_populates='item', cascade='all, delete-orphan')

