import datetime
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Text, Float
from sqlalchemy.orm import relationship
from .database import Base

class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, index=True)
    username = Column(String, unique=True, index=True)
    hashed_password = Column(String, nullable=True) # Nullable for LDAP or legacy users
    role = Column(String, default="user")  # 'admin' or 'user'
    origin = Column(String, default="local") # 'local' or 'ldap'
    
    audit_logs = relationship("AuditLog", back_populates="user")

class Category(Base):
    __tablename__ = "categories"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String, unique=True, index=True)
    description = Column(String, nullable=True)

    items = relationship("Item", back_populates="category_rel")

class Item(Base):
    __tablename__ = "items"

    id = Column(Integer, primary_key=True, index=True)
    barcode = Column(String, unique=True, index=True)
    name = Column(String, index=True)
    category = Column(String, index=True)
    category_id = Column(Integer, ForeignKey("categories.id"), nullable=True)
    type = Column(String, index=True, nullable=True)
    
    category_rel = relationship("Category", back_populates="items")
    part_number = Column(String, index=True, nullable=True)
    color = Column(String, index=True, nullable=True)
    specs = Column(Text, nullable=True)
    quantity = Column(Float, default=0.0)
    min_quantity = Column(Float, default=1.0)
    image_url = Column(String, nullable=True)
    
    # Generic box/container association for multi-item OCR scanning
    box_label = Column(String, index=True, nullable=True)
    
    # Full AI metadata
    labels_data = Column(Text, nullable=True)  

class AuditLog(Base):
    __tablename__ = "audit_logs"

    id = Column(Integer, primary_key=True, index=True)
    timestamp = Column(DateTime, default=datetime.datetime.now)
    user_id = Column(Integer, ForeignKey("users.id"))
    action = Column(String)  # e.g., 'CHECK_IN', 'CHECK_OUT', 'CREATE_ITEM'
    target_item_id = Column(Integer, nullable=True)
    target_item_name = Column(String, nullable=True)
    target_item_pn = Column(String, nullable=True)
    target_item_barcode = Column(String, nullable=True)
    target_snapshot = Column(Text, nullable=True) # Full JSON snapshot
    quantity_change = Column(Float, nullable=True)
    uuid = Column(String, unique=True, index=True, nullable=True)
    details = Column(Text, nullable=True) # For reasons, sync notes, etc.
    
    user = relationship("User", back_populates="audit_logs")

class Intervention(Base):
    __tablename__ = "interventions"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String)
    status = Column(String, default="ACTIVE")
    created_at = Column(DateTime, default=datetime.datetime.now)
    
    items = relationship("InterventionItem", back_populates="intervention")

class InterventionItem(Base):
    __tablename__ = "intervention_items"

    id = Column(Integer, primary_key=True, index=True)
    intervention_id = Column(Integer, ForeignKey("interventions.id"))
    item_id = Column(Integer, ForeignKey("items.id"))
    required_quantity = Column(Float, default=1.0)
    checked_out_quantity = Column(Float, default=0.0)
    
    intervention = relationship("Intervention", back_populates="items")

class SystemSetting(Base):
    __tablename__ = "system_settings"

    key = Column(String, primary_key=True, index=True)
    value = Column(String)
