from __future__ import annotations

from datetime import datetime

from sqlalchemy import ARRAY, CheckConstraint, DateTime, ForeignKey, String, Text, text
from sqlalchemy.orm import Mapped, mapped_column, relationship

from src.db.base import Base


class PhaseTemplate(Base):
    __tablename__ = "phase_template"
    __table_args__ = (
        CheckConstraint(text("cardinality(column_keys) > 0"), name="chk_v4_phase_template_columns"),
        CheckConstraint(text("role != 'ADMIN'"), name="chk_v4_phase_template_no_admin"),
        CheckConstraint(text("closes_at > opens_at"), name="chk_v4_phase_template_window"),
        {"schema": "v3"},
    )

    form_type: Mapped[str] = mapped_column(String, ForeignKey("v3.form_type.code"), primary_key=True)
    sheet: Mapped[str] = mapped_column(String, primary_key=True)
    phase_code: Mapped[str] = mapped_column(String, primary_key=True)
    role: Mapped[str] = mapped_column(String, ForeignKey("v3.role.code"))
    column_keys: Mapped[list[str]] = mapped_column(ARRAY(Text))
    opens_at: Mapped[datetime] = mapped_column(DateTime(timezone=False))
    closes_at: Mapped[datetime] = mapped_column(DateTime(timezone=False))

#     form_type_rel = relationship("FormType", foreign_keys=[form_type])
#     role_rel = relationship("Role", foreign_keys=[role], back_populates="phase_templates")
