from datetime import datetime
import typing

from sqlalchemy import DateTime, ForeignKey, Integer, String, func
from sqlalchemy.orm import Mapped, mapped_column, relationship

from src.db.models.form_type import FormTypeEnum
from src.db.base import Base


class BudgetForm(Base):
    __tablename__ = "budget_form"
    __table_args__ = {"schema": "v3"}

    id: Mapped[int] = mapped_column(primary_key=True)
    form_type_code: Mapped[FormTypeEnum] = mapped_column( #type: ignore
        String, ForeignKey("v3.form_type.code")
    )
    year: Mapped[int | None] = mapped_column(Integer)
    created_by: Mapped[int | None] = mapped_column(
        Integer, ForeignKey("v3.app_user.id", name="fk_v3_budget_form_created_by")
    )
    created_at: Mapped[datetime | None] = mapped_column(
        DateTime, server_default=func.now()
    )
    updated_by: Mapped[int | None] = mapped_column(
        Integer, ForeignKey("v3.app_user.id", name="fk_v3_budget_form_updated_by")
    )
    updated_at: Mapped[datetime | None] = mapped_column(
        DateTime,
        default=func.now(),
        onupdate=func.now(),
    )
    org_unit_id: Mapped[int | None] = mapped_column(
        Integer, ForeignKey("v3.org_unit.id", name="fk_v3_budget_form_org_unit")
    )

    form_type: Mapped["FormType"] = relationship(
        "FormType", #back_populates="budget_forms"
    )
#     creator: Mapped[typing.Optional["AppUser"]] = relationship(
#         "AppUser", back_populates="created_budget_forms", foreign_keys=[created_by]
#     )
#     updater: Mapped[typing.Optional["AppUser"]] = relationship(
#         "AppUser", back_populates="updated_budget_forms", foreign_keys=[updated_by]
#     )
    org_unit: Mapped[typing.Optional["OrgUnit"]] = relationship(
        "OrgUnit", #back_populates="budget_forms"
    )
#     budget_lines: Mapped[list["BudgetLine"]] = relationship(
#         "BudgetLine", back_populates="budget_form"
#     )
#     fixed_asset_reports: Mapped[list["FixedAssetReport"]] = relationship(
#         "FixedAssetReport", back_populates="budget_form"
#     )
#     form_phases: Mapped[list["FormPhase"]] = relationship(
#         "FormPhase", back_populates="budget_form"
#     )
#     form_limits: Mapped[list["FormLimit"]] = relationship(
#         "FormLimit", back_populates="budget_form"
#     )
