from sqlalchemy.orm import mapped_column, Mapped
from sqlalchemy import String, ForeignKey, Index, TIMESTAMP, SmallInteger, Identity

from databases.sql_models.core_mixin import CrudCollection


class BuildIbans(CrudCollection):
    """
    BuildParts class based on declarative_base and BaseMixin via session
    """

    __tablename__ = "build_ibans"
    __exclude__fields__ = []

    iban: Mapped[str] = mapped_column(
        String(40), server_default="", nullable=False, comment="IBAN number"
    )
    start_date: Mapped[TIMESTAMP] = mapped_column(
        TIMESTAMP(timezone=True), nullable=False, comment="Bank Transaction Start Date"
    )

    stop_date: Mapped[TIMESTAMP] = mapped_column(
        TIMESTAMP(timezone=True), server_default="2900-01-01 00:00:00"
    )
    bank_code: Mapped[str] = mapped_column(String(24), server_default="TR0000000000000")
    xcomment: Mapped[str] = mapped_column(String(64), server_default="????")

    build_id: Mapped[int] = mapped_column(
        ForeignKey("build.id"), nullable=True, comment="Building ID"
    )
    build_uu_id: Mapped[str] = mapped_column(
        String, nullable=True, comment="Building UUID", index=True
    )
    # building: Mapped["Build"] = relationship(
    #     "Build", back_populates="build_ibans", foreign_keys=[build_id]
    # )

    __table_args__ = (
        Index("_build_ibans_ndx_01", iban, start_date, unique=True),
        {"comment": "IBANs related to money transactions due to building objects"},
    )

    # @property
    # def enums(self):
    #     return_dict = {}
    #     for key, enum in self.__enums_list__.items():
    #         for enum_item in EnumDropdown.filter_by(enum_class=enum):
    #             return_dict[key] = {
    #                 enum_item.get_dict(include=["key", "value", "description"])
    #             }
    #     return return_dict


class BuildIbanDescription(CrudCollection):
    """
    SearchComments class based on declarative_base and CrudCollection via session
    """

    __tablename__ = "build_iban_description"
    __exclude__fields__ = []

    iban: Mapped[str] = mapped_column(String, nullable=False, comment="IBAN Number")
    group_id: Mapped[int] = mapped_column(
        SmallInteger, nullable=False, comment="Group ID"
    )
    search_word: Mapped[str] = mapped_column(
        String, nullable=False, comment="Search Word", index=True
    )

    # decision_book_project_id: Mapped[int] = mapped_column(
    #     ForeignKey("build_decision_book_projects.id")
    # )
    # decision_book_project_uu_id: Mapped[str] = mapped_column(
    #     String, nullable=False, comment="Decision Book Project UUID"
    # )
    customer_id: Mapped[int] = mapped_column(ForeignKey("people.id"), nullable=True)
    customer_uu_id: Mapped[str] = mapped_column(
        String, nullable=True, comment="Customer UUID"
    )
    company_id: Mapped[int] = mapped_column(ForeignKey("companies.id"), nullable=True)
    company_uu_id: Mapped[str] = mapped_column(
        String, nullable=True, comment="Company UUID"
    )
    build_parts_id: Mapped[int] = mapped_column(
        ForeignKey("build_parts.id"), nullable=True
    )
    build_parts_uu_id: Mapped[str] = mapped_column(
        String, nullable=True, comment="Build Parts UUID"
    )

    # decision_book_project: Mapped["BuildDecisionBookProjects"] = relationship(
    #     "BuildDecisionBookProjects",
    #     back_populates="search_iban_description",
    #     foreign_keys=[decision_book_project_id],
    # )
    # customer: Mapped["People"] = relationship(
    #     "People", back_populates="search_iban_description", foreign_keys=[customer_id]
    # )
    # company: Mapped["Companies"] = relationship(
    #     "Company", back_populates="search_iban_description", foreign_keys=[company_id]
    # )
    # parts: Mapped["BuildParts"] = relationship(
    #     "BuildParts",
    #     back_populates="search_iban_description",
    #     foreign_keys=[build_parts_id],
    # )

    __table_args__ = (
        Index(
            "_search_iban_description_ndx_00", iban, search_word, group_id, unique=True
        ),
        {"comment": "Search Iban Description Information"},
    )
