from typing import Optional
from sqlalchemy import Integer, String, UniqueConstraint, Boolean, ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship
from api.v1.common.base_models import TenantScopedMixin, TimestampMixin
from core.database import Base
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from api.v1.modules.a76.general_catalogs.classification_concepts.models import ClassificationConcept


class Concept(Base, TenantScopedMixin, TimestampMixin):
    __tablename__ = "concepts"
    __table_args__ = (
        UniqueConstraint("code", name="uq_concept_code"),
        {"schema": "a76"}
    )

    id: Mapped[int] = mapped_column(
        Integer, primary_key=True, autoincrement=True)

    company_id: Mapped[int] = mapped_column(
        Integer, nullable=False)  # IDEMPRESA

    code: Mapped[str] = mapped_column(String(15), nullable=False)  # CLAVE

    description: Mapped[Optional[str]] = mapped_column(
        String(120), nullable=True)  # DESCRIPCION

    description_en: Mapped[Optional[str]] = mapped_column(
        String(120), nullable=True)  # DESCRIPCIONINGLES

    detailed_description: Mapped[Optional[str]] = mapped_column(
        String(1000), nullable=True)  # DESCRIPCIONDETALLADA

    priority: Mapped[Optional[int]] = mapped_column(
        Integer, nullable=True)  # PRIORIDAD

    priority_ame: Mapped[Optional[int]] = mapped_column(
        Integer, nullable=True)  # PRIORIDADAME

    first_total: Mapped[Optional[bool]] = mapped_column(
        Boolean, nullable=True)  # PRIMERTOTAL

    type: Mapped[Optional[str]] = mapped_column(
        String(9), nullable=True)  # TIPO

    is_printed: Mapped[Optional[bool]] = mapped_column(
        Boolean, nullable=True)  # SEIMPRIME

    section: Mapped[Optional[int]] = mapped_column(
        Integer, nullable=True)  # SECCION
        
    classification: Mapped[Optional[str]] = mapped_column(String(30), ForeignKey(
        "a76.classification_concepts.classification"), nullable=True)  # CLASIFICACION

    classification_info: Mapped[Optional["ClassificationConcept"]] = relationship(
        foreign_keys=[classification])
