from datetime import datetime
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime, Float, UniqueConstraint
from sqlalchemy.orm import relationship
from src.utils.db import Base, engine

# Stores Table
class Store(Base):
    __tablename__ = "stores"

    store_id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.user_id"), nullable=False)
    storetype_id = Column(Integer, ForeignKey("store_type.storetype_id"), nullable=False)
    name = Column(String(255), unique=True, nullable=False)
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    branch_limit = Column(Integer, nullable=False, default=2) 

    # Relationships
    user = relationship("User", back_populates="stores")
    branches = relationship("Branch", back_populates="store")
    datasources = relationship("Datasource", back_populates="store")
    feedbacks = relationship("Feedback", back_populates="store")
    tasks = relationship("Task", back_populates="store")
    words = relationship("Word", back_populates="store")
    emotions = relationship("Emotion", back_populates="store")
    review_topics = relationship("ReviewTopic", back_populates="store")
    store_type = relationship("StoreType", back_populates="store")

# Branches Table
class Branch(Base):
    __tablename__ = "branches"

    branch_id = Column(Integer, primary_key=True, index=True)
    store_id = Column(Integer, ForeignKey("stores.store_id", ondelete="CASCADE"), nullable=False)
    user_id = Column(Integer, ForeignKey("users.user_id", ondelete="CASCADE"), nullable=False)
    branch_name = Column(String(255), nullable=False)
    address = Column(String(500), nullable=False)
    contact_info = Column(String(20), nullable=False)
    overall_sentiment = Column(String(50), nullable=True)
    overall_sentiment_score = Column(Float, nullable=True)
    average_rating = Column(Float, nullable=True)
    total_review_count = Column(Integer, nullable=True)
    avg_review_count = Column(Float, nullable=True)
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    # Relationships
    store = relationship("Store", back_populates="branches")
    datasources = relationship("Datasource", back_populates="branch")
    feedbacks = relationship("Feedback", back_populates="branch")
    tasks = relationship("Task", back_populates="branch")
    words = relationship("Word", back_populates="branch")
    emotions = relationship("Emotion", back_populates="branch")
    review_topics = relationship("ReviewTopic", back_populates="branch")
    recommendations = relationship("Recommendation", back_populates="branch")


class StoreType(Base):
    __tablename__ = "store_type"
    storetype_id = Column(Integer, primary_key=True, index=True)
    store_type = Column(String, unique=True, nullable=False)
    created_at = Column(DateTime, default=datetime.utcnow)

    # Relationships
    store = relationship("Store", back_populates="store_type")
    topic = relationship("Topic", back_populates="store_type")
