from sqlalchemy import Column, Integer, String, Text, Boolean, ForeignKey, JSON
from utils.mysql_db import Base
from mixins.timestamp_mixin import TimestampMixin
from uuid import uuid4
from datetime import datetime
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func


class BuyerIntake(Base, TimestampMixin):
    __tablename__ = "buyer_intake_form"

    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    first_name = Column(String(100), nullable=False)
    last_name = Column(String(100), nullable=False)
    email = Column(String(255), unique=True, nullable=False)
    phone = Column(String(20))
    additional_info = Column(Text)
    deal_size = Column(String(50))
    revenue_multiple = Column(String(50))
    ebidta_multiple = Column(String(50))
    irr = Column(String(50))
    is_financing_needed = Column(Boolean, default=False)
    time_frame = Column(String(100))

    locations = relationship(
        "BuyerLocation", back_populates="buyer", cascade="all, delete"
    )
    industries = relationship(
        "BuyerIndustry", back_populates="buyer", cascade="all, delete"
    )

    def to_dict(self):
        return {
            "id": self.id,
            "first_name": self.first_name,
            "last_name": self.last_name,
            "email": self.email,
            "phone": self.phone,
            "additional_info": self.additional_info,
            "deal_size": self.deal_size,
            "revenue_multiple": self.revenue_multiple,
            "ebidta_multiple": self.ebidta_multiple,
            "irr": self.irr,
            "is_financing_needed": self.is_financing_needed,
            "time_frame": self.time_frame,
            # "locations": [location.to_dict() for location in self.locations],
            # "industries": [industry.to_dict() for industry in self.industries],
        }

    def update_modify(self, user_id):
        """Update the modified date"""
        self.modified_at = datetime.now()
        self.modified_by = user_id


class BuyerLocation(Base, TimestampMixin):
    __tablename__ = "buyer_intake_locations"

    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    buyer_intake_id = Column(Integer, ForeignKey("buyer_intake_form.id"))
    location = Column(String(100))

    buyer = relationship("BuyerIntake", back_populates="locations")

    def update_modify(self, user_id):
        """Update the modified date"""
        self.modified_at = datetime.now()
        self.modified_by = user_id
        return self


class BuyerIndustry(Base, TimestampMixin):
    __tablename__ = "buyer_intake_industries"

    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    buyer_intake_id = Column(Integer, ForeignKey("buyer_intake_form.id"))
    industry = Column(String(100))

    buyer = relationship("BuyerIntake", back_populates="industries")

    def update_modify(self, user_id):
        """Update the modified date"""
        self.modified_at = datetime.now()
        self.modified_by = user_id
        return self
