""" Lead Model - Leads generated from social media interactions. """ from datetime import datetime from sqlalchemy import Column, Integer, String, Text, Boolean, DateTime, ForeignKey, JSON, Enum from sqlalchemy.dialects.postgresql import ARRAY from sqlalchemy.orm import relationship import enum from app.core.database import Base class LeadStatus(enum.Enum): """Lead status options.""" NEW = "new" CONTACTED = "contacted" QUALIFIED = "qualified" PROPOSAL = "proposal" WON = "won" LOST = "lost" class LeadPriority(enum.Enum): """Lead priority levels.""" LOW = "low" MEDIUM = "medium" HIGH = "high" URGENT = "urgent" class Lead(Base): """ Lead model for tracking potential customers from social media. """ __tablename__ = "leads" id = Column(Integer, primary_key=True, index=True) # Source information interaction_id = Column(Integer, ForeignKey("interactions.id"), nullable=True) platform = Column(String(50), nullable=False, index=True) # Contact information name = Column(String(255), nullable=True) email = Column(String(255), nullable=True, index=True) phone = Column(String(50), nullable=True) company = Column(String(255), nullable=True) # Social media info username = Column(String(100), nullable=True) profile_url = Column(String(500), nullable=True) # Interest and context interest = Column(Text, nullable=True) # What they're interested in source_content = Column(Text, nullable=True) # Original interaction content notes = Column(Text, nullable=True) # Products/services interest products_interested = Column(ARRAY(Integer), nullable=True) # Product IDs services_interested = Column(ARRAY(Integer), nullable=True) # Service IDs # Status tracking status = Column(String(20), default="new", index=True) priority = Column(String(20), default="medium", index=True) # Assignment assigned_to = Column(String(100), nullable=True) # Odoo integration odoo_lead_id = Column(Integer, nullable=True, unique=True, index=True) synced_to_odoo = Column(Boolean, default=False) odoo_synced_at = Column(DateTime, nullable=True) # Metadata tags = Column(ARRAY(String), nullable=True) custom_fields = Column(JSON, nullable=True) # Timestamps created_at = Column(DateTime, default=datetime.utcnow) updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) last_contacted_at = Column(DateTime, nullable=True) # Relationship interaction = relationship("Interaction", backref="leads") def __repr__(self): return f"" def to_dict(self): """Convert to dictionary.""" return { "id": self.id, "interaction_id": self.interaction_id, "platform": self.platform, "name": self.name, "email": self.email, "phone": self.phone, "company": self.company, "username": self.username, "profile_url": self.profile_url, "interest": self.interest, "source_content": self.source_content, "notes": self.notes, "products_interested": self.products_interested, "services_interested": self.services_interested, "status": self.status, "priority": self.priority, "assigned_to": self.assigned_to, "odoo_lead_id": self.odoo_lead_id, "synced_to_odoo": self.synced_to_odoo, "tags": self.tags, "created_at": self.created_at.isoformat() if self.created_at else None, "updated_at": self.updated_at.isoformat() if self.updated_at else None, "last_contacted_at": self.last_contacted_at.isoformat() if self.last_contacted_at else None }