# app/models/financial_information_template.py
from sqlalchemy import Column, Integer, String, Text, Boolean, ForeignKey
from utils.mysql_db import Base
from sqlalchemy.dialects.mysql import JSON
from sqlalchemy.orm import relationship
from mixins.timestamp_mixin import TimestampMixin
import socket
from datetime import datetime


class FinancialInformationTemplate(Base, TimestampMixin):
    __tablename__ = "financial_information_template"

    template_id = Column(Integer, primary_key=True, index=True)
    name = Column(String(255), nullable=False)
    client_id = Column(String(255), nullable=True)
    s3_path = Column(Text, nullable=True)
    is_active = Column(Boolean, nullable=True, default=True)
    pages = relationship(
        "FinancialInformationPage",
        back_populates="template",
        cascade="all, delete-orphan",
    )


class FinancialInformationPage(Base, TimestampMixin):
    __tablename__ = "financial_information_pages"

    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    project_id = Column(String(255), nullable=False)
    name = Column(String(100), nullable=False)
    template_type = Column(
        Integer, ForeignKey("financial_information_template.template_id"), nullable=True
    )
    subheadings = Column(String(255), nullable=True)
    currency = Column(String(10), nullable=True)
    meta = Column(JSON, nullable=True)
    is_active = Column(Boolean, default=True)
    template = relationship("FinancialInformationTemplate", back_populates="pages")
    related_docs = relationship(
        "FinancialInformationRelatedDoc",
        back_populates="page",
        cascade="all, delete-orphan",
    )
    financial_information_tables = relationship(
        "FinancialInformationTable", back_populates="page", cascade="all, delete-orphan"
    )

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

    def to_dict(self):
        "Convert the class to a dictionary"
        return {
            "id": self.id,
            "project_id": self.project_id,
            "name": self.name,
            "template_type": self.template_type,
            "subheadings": self.subheadings,
            "currency": self.currency,
            "meta": self.meta,
            "created_at": self.created_at,
            "modified_at": self.modified_at,
            "created_by": self.created_by,
            "modified_by": self.modified_by,
            "is_active": self.is_active,
        }


class FinancialInformationRelatedDoc(Base, TimestampMixin):
    __tablename__ = "financial_information_related_docs"

    id = Column("ID", Integer, primary_key=True, index=True)
    project_id = Column(String(255), nullable=False)
    page_id = Column(
        Integer, ForeignKey("financial_information_pages.id"), nullable=False
    )
    name = Column(String(255), nullable=False)
    path = Column(String(255), nullable=False)
    doc_type = Column(String(50), nullable=True)

    page = relationship("FinancialInformationPage", back_populates="related_docs")


class FinancialInformationTable(Base, TimestampMixin):
    __tablename__ = "financial_information_table"

    id = Column(Integer, primary_key=True, index=True)
    page_id = Column(
        Integer,
        ForeignKey("financial_information_pages.id"),
        nullable=False,
        unique=True,
    )
    project_id = Column(String(255), nullable=False)

    data = Column(JSON, nullable=False)
    meta = Column(JSON, nullable=True)

    page = relationship(
        "FinancialInformationPage",
        back_populates="financial_information_tables",
        uselist=False,
    )

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

    def to_dict(self):
        "Convert the class to a dictionary"
        return {
            "id": self.id,
            "page_id": self.page_id,
            "data": self.data,
            "meta": self.meta,
            "created_at": self.created_at,
            "modified_at": self.modified_at,
            "created_by": self.created_by,
            "modified_by": self.modified_by,
            "project_id": self.project_id,
        }
