import socket

from sqlalchemy import TIMESTAMP, Boolean, Column, ForeignKey, Integer, String
from sqlalchemy.orm import relationship

from utils.mysql_db import Base


class BuyersTargetsInfo(Base):
    "Buyers targets sql model"
    __tablename__ = "buyers_targets_info"

    id = Column(Integer, primary_key=True, autoincrement=True)
    company_id = Column(String(255), nullable=False, unique=True)
    company_url = Column(String(500), nullable=False)
    project_id = Column(String(255), nullable=False)
    description = Column(String(1000))
    rationale = Column(String(2000))
    logo_url = Column(String(500))
    record_type_id = Column(Integer, ForeignKey("buyers_targets_record_type.id"))
    created_at = Column(TIMESTAMP, server_default="CURRENT_TIMESTAMP")
    company_name = Column(String(255), nullable=False)
    updated_at = Column(
        TIMESTAMP, server_default="CURRENT_TIMESTAMP", onupdate="CURRENT_TIMESTAMP"
    )
    notes = Column(String(2000))
    is_active = Column(Boolean, default=False)
    category_name = Column(String(255))
    created_by = Column(String(255), nullable=False, default=socket.gethostname())
    modified_by = Column(String(255), nullable=True, default=socket.gethostname())

    record_type = relationship(
        "BuyersTargetsRecordType", back_populates="buyers_targets_info"
    )

    @classmethod
    async def insert_new_record(cls, db, **kwargs):
        """
        Insert a new record into the buyers_targets_info table using MySQLDB's execute_insert.

        :param db: An instance of MySQLDB
        :param kwargs: Dictionary of column values for the new record
        """
        new_record = cls(
            **kwargs,
            created_by=socket.gethostname(),
        )
        record = await db.execute_insert(new_record)
        return record

    def to_dict(self):
        "Convert the class to a dictionary"
        return {
            "id": self.id,
            "company_id": self.company_id,
            "company_url": self.company_url,
            "project_id": self.project_id,
            "description": self.description,
            "rationale": self.rationale,
            "logo_url": self.logo_url,
            "record_type_id": self.record_type_id,
            "created_at": self.created_at,
            "updated_at": self.updated_at,
            "notes": self.notes,
            "is_active": self.is_active,
            "company_name": self.company_name,
            "category_name": self.category_name,
        }


class BuyersTargetsRecordType(Base):
    "Buyers targets record type sql model"
    __tablename__ = "buyers_targets_record_type"

    id = Column(Integer, primary_key=True, autoincrement=True)
    record_type_name = Column(String(255), nullable=False)
    label = Column(String(25), nullable=False)
    subHeading = Column(String(255), nullable=False)
    pitchType = Column(String(25), nullable=False)
    searchType = Column(String(25), nullable=False)

    buyers_targets_info = relationship(
        "BuyersTargetsInfo", back_populates="record_type"
    )

    def to_dict(self):
        "Convert the class to a dictionary"
        return {
            "id": self.id,
            "record_type_name": self.record_type_name,
            "label": self.label,
            "subHeading": self.subHeading,
            "pitchType": self.pitchType,
            "searchType": self.searchType,
        }
