from __future__ import annotations

from datetime import datetime

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

from src.db.base import Base


class RfProjectReport(Base):
    __tablename__ = "rf_project_report"
    __table_args__ = (
        UniqueConstraint("project_id", "year", "report_type", name="uq_v3_rf_project_report"),
        Index("ix_v3_rf_project_report_proj_year", "project_id", "year"),
        {"schema": "v3"},
    )

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    project_id: Mapped[int] = mapped_column(Integer, ForeignKey("v3.project.id"))
    year: Mapped[int] = mapped_column(Integer)
    report_type: Mapped[str] = mapped_column(String, CheckConstraint("report_type IN ('LIMIT','CURRENT_EXPENSES')"))
    created_by: Mapped[int | None] = mapped_column(Integer, ForeignKey("v3.app_user.id"))
    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"))
    updated_at: Mapped[datetime | None] = mapped_column(DateTime, server_default=func.now())

    project: Mapped["Project"] = relationship("Project", back_populates="rf_project_reports") # type: ignore
#     creator: Mapped[AppUser | None] = relationship("AppUser", foreign_keys=[created_by])
#     updater: Mapped[AppUser | None] = relationship("AppUser", foreign_keys=[updated_by])
#     lines: Mapped[list[RfProjectReportLine]] = relationship("RfProjectReportLine", back_populates="report")
