from sqlalchemy import (
    String,
    ForeignKey,
    Index,
    SmallInteger,
    Boolean,
    TIMESTAMP,
    Text,
    Numeric,
    Integer,
)
from sqlalchemy.orm import mapped_column, Mapped, relationship
from databases.sql_models.core_mixin import CrudCollection


class DecisionBookBudgetBooks(CrudCollection):

    __tablename__ = "decision_book_budget_books"
    __exclude__fields__ = []

    country: Mapped[str] = mapped_column(String, nullable=False)
    branch_type: Mapped[int] = mapped_column(SmallInteger, server_default="0")

    company_id: Mapped[int] = mapped_column(ForeignKey("companies.id"), nullable=False)
    company_uu_id: Mapped[str] = mapped_column(String, nullable=False)
    branch_id: Mapped[int] = mapped_column(ForeignKey("companies.id"), nullable=True)
    branch_uu_id: Mapped[str] = mapped_column(
        String, comment="Branch UU ID", nullable=True
    )
    build_decision_book_id: Mapped[int] = mapped_column(
        ForeignKey("build_decision_book.id"), nullable=False
    )
    build_decision_book_uu_id: Mapped[str] = mapped_column(
        String, nullable=True, comment="Build Decision Book UU ID"
    )

    __table_args__ = (
        Index(
            "_decision_book_budget_companies_book_ndx_00",
            company_id,
            CrudCollection.created_at,
        ),
        {"comment": "budget Book Information"},
    )


class DecisionBookBudgetCodes(CrudCollection):

    __tablename__ = "decision_book_budget_codes"
    __exclude__fields__ = []

    budget_code: Mapped[str] = mapped_column(
        String(48), nullable=False, comment="budget Code"
    )
    comment_line: Mapped[str] = mapped_column(
        Text, nullable=False, comment="Comment Line"
    )

    build_decision_book_id: Mapped[int] = mapped_column(
        ForeignKey("build_decision_book.id"), nullable=True
    )
    build_decision_book_uu_id: Mapped[str] = mapped_column(
        String, nullable=True, comment="Build Decision Book UU ID"
    )

    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 UU ID"
    )

    company_id: Mapped[int] = mapped_column(ForeignKey("companies.id"), nullable=True)
    company_uu_id: Mapped[str] = mapped_column(
        String, nullable=True, comment="Company UU ID"
    )

    __table_args__ = (
        Index("_decision_book_budget_codes_ndx_00", budget_code, "created_at"),
        Index("_decision_book_budget_codes_ndx_01", company_id, "created_at"),
        {"comment": "budget Book Information"},
    )


class DecisionBookBudgetMaster(CrudCollection):

    __tablename__ = "decision_book_budget_master"
    __exclude__fields__ = []

    budget_type: Mapped[str] = mapped_column(
        String(50), nullable=False
    )  # Bütçe tipi (örneğin: Operasyonel, Yatırım)
    currency: Mapped[str] = mapped_column(
        String(8), server_default="TRY"
    )  # Bütçe para birimi
    total_budget: Mapped[float] = mapped_column(
        Numeric(10, 2), nullable=False
    )  # Toplam bütçe

    tracking_period_id: Mapped[int] = mapped_column(
        ForeignKey("api_enum_dropdown.id"), nullable=True
    )
    tracking_period_uu_id: Mapped[str] = mapped_column(
        String, nullable=True, comment="Part Direction UUID"
    )
    budget_books_id: Mapped[int] = mapped_column(
        Integer, ForeignKey("decision_book_budget_books.id"), nullable=False
    )
    budget_books_uu_id: Mapped[str] = mapped_column(
        String, nullable=True, comment="Budget Books UU ID"
    )
    department_id: Mapped[int] = mapped_column(
        Integer, ForeignKey("departments.id"), nullable=False
    )  # Departman ile ilişki
    department_uu_id: Mapped[str] = mapped_column(
        String, nullable=True, comment="Department UU ID"
    )

    __table_args__ = ({"comment": "budget Book Information"},)


class DecisionBookBudgets(CrudCollection):

    __tablename__ = "decision_book_budgets"
    __exclude__fields__ = []

    process_date: Mapped[TIMESTAMP] = mapped_column(
        TIMESTAMP(timezone=True), nullable=False
    )  # Başlangıç tarihi
    budget_codes_id: Mapped[int] = mapped_column(
        Integer, ForeignKey("decision_book_budget_codes.id"), nullable=False
    )
    total_budget: Mapped[float] = mapped_column(
        Numeric(10, 2), nullable=False
    )  # Toplam bütçe
    used_budget: Mapped[float] = mapped_column(
        Numeric(10, 2), nullable=False, default=0.0
    )  # Kullanılan bütçe
    remaining_budget: Mapped[float] = mapped_column(
        Numeric(10, 2), nullable=False, default=0.0
    )  # Kullanılan bütçe

    decision_book_budget_master_id: Mapped[int] = mapped_column(
        Integer, ForeignKey("decision_book_budget_master.id"), nullable=False
    )
    decision_book_budget_master_uu_id: Mapped[str] = mapped_column(
        String, nullable=True, comment="Decision Book Budget Master UU ID"
    )

    __table_args__ = (
        Index(
            "_decision_book_budgets_ndx_00",
            decision_book_budget_master_uu_id,
            process_date,
        ),
        {"comment": "budget Book Information"},
    )
