from sqlalchemy import Column, Integer, String, ForeignKey, Text, DateTime, func
from sqlalchemy.orm import relationship
from src.utils.db import Base

class Emotion(Base):
    __tablename__ = "emotions"

    emotion_id = Column(Integer, primary_key=True, index=True)
    store_id = Column(Integer, ForeignKey("stores.store_id"), nullable=False)
    datasource_id = Column(Integer, ForeignKey("datasource.ds_id"), nullable=False)
    feedback_id = Column(Integer, ForeignKey("feedbacks.feedback_id"), nullable=False)
    branch_id = Column(Integer, ForeignKey("branches.branch_id"), nullable=False) 
    emotions = Column(String(100), nullable=False) 
    created_at = Column(DateTime, default=func.now())

    # Relationships
    store = relationship("Store", back_populates="emotions")
    datasource = relationship("Datasource", back_populates="emotions")
    branch = relationship("Branch", back_populates="emotions")
    feedbacks = relationship("Feedback", back_populates="emotions")

class Word(Base):
    __tablename__ = "words"

    word_id = Column(Integer, primary_key=True, index=True)
    store_id = Column(Integer, ForeignKey("stores.store_id"), nullable=False)
    datasource_id = Column(Integer, ForeignKey("datasource.ds_id"), nullable=False)
    feedback_id = Column(Integer, ForeignKey("feedbacks.feedback_id"), nullable=False)
    branch_id = Column(Integer, ForeignKey("branches.branch_id"), nullable=False) 
    words = Column(String(100), nullable=False) 
    sentiment = Column(String(20), nullable=True)  # "positive", "negative", "neutral", "mixed"
    created_at = Column(DateTime, default=func.now())

    # Relationships
    store = relationship("Store", back_populates="words")
    datasource = relationship("Datasource", back_populates="words")
    branch = relationship("Branch", back_populates="words")
    feedbacks = relationship("Feedback", back_populates="words")
   

class Topic(Base):
    __tablename__ = "topics"

    topic_id = Column(Integer, primary_key=True, index=True)
    topic_name = Column(Text, unique=True, nullable=False)
    storetype_id = Column(Integer, ForeignKey("store_type.storetype_id"), nullable=True)
    topic_category = Column(Text, nullable=False)
    created_at = Column(DateTime, default=func.now())

    # Relationships
    review_topics = relationship("ReviewTopic", back_populates="topic")
    store_type = relationship("StoreType", back_populates="topic")

class ReviewTopic(Base):
    __tablename__ = "review_topics"

    rt_id = Column(Integer, primary_key=True, index=True)
    store_id = Column(Integer, ForeignKey("stores.store_id"), nullable=False)
    datasource_id = Column(Integer, ForeignKey("datasource.ds_id"), nullable=False)
    feedback_id = Column(Integer, ForeignKey("feedbacks.feedback_id"), nullable=False)
    branch_id = Column(Integer, ForeignKey("branches.branch_id"), nullable=False) 
    topic_id = Column(Integer, ForeignKey("topics.topic_id"), nullable=False)
    topic_name = Column(String(100), nullable=True) 
    topic_sentiment = Column(String(20), nullable=True)  # "positive", "negative", "neutral", "mixed"
    created_at = Column(DateTime, default=func.now())

    # Relationships
    topic = relationship("Topic", back_populates="review_topics")
    store = relationship("Store", back_populates="review_topics")
    datasource = relationship("Datasource", back_populates="review_topics")
    feedbacks = relationship("Feedback", back_populates="review_topics")
    branch = relationship("Branch", back_populates="review_topics")
   
