from datetime import datetime, timezone
from sqlalchemy import String, DateTime, Integer, ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.core.database import Base


class Contract(Base):
    __tablename__ = "contracts"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    contract_number: Mapped[str] = mapped_column(String(50), nullable=False)
    year: Mapped[int] = mapped_column(Integer, nullable=False)
    company: Mapped[str] = mapped_column(String(100), nullable=False)
    area: Mapped[str | None] = mapped_column(String(100))
    plate: Mapped[str | None] = mapped_column(String(10))
    customer_name: Mapped[str | None] = mapped_column(String(200))
    import_batch: Mapped[str | None] = mapped_column(String(100))
    imported_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), default=lambda: datetime.now(timezone.utc)
    )

    valuation_contracts: Mapped[list["ValuationContract"]] = relationship(
        "ValuationContract", back_populates="contract"
    )


class ValuationContract(Base):
    __tablename__ = "valuation_contracts"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    valuation_id: Mapped[int] = mapped_column(Integer, ForeignKey("valuations.id"), nullable=False)
    contract_id: Mapped[int] = mapped_column(Integer, ForeignKey("contracts.id"), nullable=False)
    linked_by: Mapped[int] = mapped_column(Integer, ForeignKey("users.id"), nullable=False)
    linked_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), default=lambda: datetime.now(timezone.utc)
    )

    valuation: Mapped["Valuation"] = relationship("Valuation", back_populates="contracts")
    contract: Mapped["Contract"] = relationship("Contract", back_populates="valuation_contracts")


from app.models.valuation import Valuation
