from __future__ import annotations

from sqlalchemy import CheckConstraint, ForeignKey, Index, Integer, String
from sqlalchemy.orm import Mapped, mapped_column

from src.db.base import Base


class ExpenseItem(Base):
    __tablename__ = "expense_item"
    __table_args__ = (
        CheckConstraint("direction IN ('Support', 'Development')", name="expense_item_direction_check"),
        CheckConstraint("sheet IN ('AHR', 'CAP', 'OPER')", name="expense_item_sheet_check"),
        Index("ix_v3_expense_item_parent", "parent_id"),
        Index("ix_v3_expense_item_sheet", "sheet"),
        {"schema": "v3"},
    )

    id: Mapped[int] = mapped_column(primary_key=True)
    section_code: Mapped[str | None] = mapped_column(String)
    item_id: Mapped[str | None] = mapped_column(String)
    num_group_id: Mapped[str | None] = mapped_column(String)
    name: Mapped[str | None] = mapped_column(String)
    sheet: Mapped[str | None] = mapped_column(String)
    direction: Mapped[str | None] = mapped_column(String)
    parent_id: Mapped[int | None] = mapped_column(Integer, ForeignKey("v3.expense_item.id"))
    depth: Mapped[int | None] = mapped_column(Integer, default=0)



class ExpenseItemFormType(Base):
    __tablename__ = "expense_item_form_type"
    __table_args__ = {"schema": "v3"}

    form_type_code: Mapped[str] = mapped_column(String, primary_key=True)
    expense_item_id: Mapped[int] = mapped_column(Integer, ForeignKey("v3.expense_item.id"), primary_key=True)
