from __future__ import annotations

from datetime import datetime

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

from src.db.base import Base


class Project(Base):
    __tablename__ = "project"
    __table_args__ = (
        CheckConstraint("level IN ('program', 'project')", name="project_level_check"),
        CheckConstraint(
            "placement_type IS NULL OR placement_type IN ('own', 'rent', 'other')",
            name="project_placement_type_check",
        ),
        CheckConstraint(
            "project_type IS NULL OR project_type IN ('current', 'development')",
            name="project_project_type_check",
        ),
        CheckConstraint(
            "vsp_format IS NULL OR vsp_format IN ('office', 'standalone', 'atm', 'other')",
            name="project_vsp_format_check",
        ),
        CheckConstraint(
            text("name IS NOT NULL AND name <> ''"),
            name="chk_v3_project_name",
        ),
        Index("ix_v3_project_ssp", "org_unit_id"),
        {"schema": "v3"},
    )

    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str | None] = mapped_column(String)
    level: Mapped[str | None] = mapped_column(String)
    parent_id: Mapped[int | None] = mapped_column(Integer, ForeignKey("v3.project.id"))
    project_type: Mapped[str | None] = mapped_column(String)
    vsp_format: Mapped[str | None] = mapped_column(String)
    placement_type: Mapped[str | None] = mapped_column(String)
    object_address: Mapped[str | None] = mapped_column(String)
    staff_count: Mapped[int | None] = mapped_column(Integer)
    total_area: Mapped[float | None] = mapped_column(Numeric)
    org_unit_id: Mapped[int | None] = mapped_column(Integer, ForeignKey("v3.org_unit.id"))
    created_at: Mapped[datetime | None] = mapped_column(DateTime, server_default=func.now())

#     parent = relationship("Project", remote_side="Project.id", back_populates="children")
#     children: Mapped[list["Project"]] = relationship("Project", back_populates="parent")  # type: ignore
#     org_unit = relationship("OrgUnit", back_populates="projects")
#     budget_lines: Mapped[list["BudgetLine"]] = relationship("BudgetLine", back_populates="project")  # type: ignore
    rf_project_reports: Mapped[list["RfProjectReport"]] = relationship("RfProjectReport", back_populates="project")  # type: ignore
