from __future__ import annotations

from datetime import datetime

from sqlalchemy import BigInteger, DateTime, Index, Integer, String, Text, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column

from app.models.base import Base


class DocumentArchive(Base):
    __tablename__ = "document_archives"
    __table_args__ = (
        UniqueConstraint(
            "document_type",
            "business_id",
            "archive_version",
            "file_format",
            name="uk_document_archives_version",
        ),
        Index("idx_document_archives_business_latest", "document_type", "business_id", "archive_version"),
        Index("idx_document_archives_document_no", "document_no"),
        Index("idx_document_archives_status", "status", "created_at"),
    )

    id: Mapped[int] = mapped_column(BigInteger().with_variant(Integer, "sqlite"), primary_key=True, autoincrement=True)
    document_type: Mapped[str] = mapped_column(String(50))
    business_id: Mapped[int] = mapped_column(BigInteger)
    document_no: Mapped[str] = mapped_column(String(100))
    archive_version: Mapped[int] = mapped_column(Integer)
    template_version: Mapped[str] = mapped_column(String(50))
    file_format: Mapped[str] = mapped_column(String(20))
    file_name: Mapped[str] = mapped_column(String(255))
    file_path: Mapped[str] = mapped_column(String(500))
    file_hash: Mapped[str | None] = mapped_column(String(128), nullable=True)
    status: Mapped[str] = mapped_column(String(32))
    error_message: Mapped[str | None] = mapped_column(Text, nullable=True)
    created_by: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    created_at: Mapped[datetime] = mapped_column(DateTime)
    updated_at: Mapped[datetime] = mapped_column(DateTime)
