from datetime import date, datetime

from sqlalchemy import Date, DateTime, Index, String, Text
from sqlalchemy.orm import Mapped, mapped_column

from app.database import Base


class DataImportRun(Base):
    """One row per bulk-import attempt (SEC facts / Dolt earnings / Dolt stocks).

    Lean audit record for the batch import framework: every attempt is logged,
    whether it promoted, was a ``no_op`` (unchanged revision), or ``failed``.
    ``row_counts`` and ``validation`` hold JSON strings (repo convention — see
    ``fundamental_data.unavailable_fields_json``), not JSONB; the validation
    blob carries reconciliation/discrepancy summaries so no separate conflicts
    table is needed. One run per source at a time is enforced at write time by a
    Postgres advisory lock keyed by ``source``.
    """

    __tablename__ = "data_import_runs"
    __table_args__ = (
        Index("ix_data_import_runs_source_started", "source", "started_at"),
    )

    id: Mapped[int] = mapped_column(primary_key=True)
    # sec_facts | dolt_earnings | dolt_stocks
    source: Mapped[str] = mapped_column(String(32), nullable=False)
    # Dolt commit hash, or SEC archive SHA-256. Null until known.
    revision: Mapped[str | None] = mapped_column(String(64), nullable=True)
    # running | validated | promoted | no_op | failed
    status: Mapped[str] = mapped_column(String(16), nullable=False)
    source_max_date: Mapped[date | None] = mapped_column(Date, nullable=True)
    row_counts_json: Mapped[str | None] = mapped_column(Text, nullable=True)
    validation_json: Mapped[str | None] = mapped_column(Text, nullable=True)
    started_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), default=datetime.utcnow, nullable=False
    )
    completed_at: Mapped[datetime | None] = mapped_column(
        DateTime(timezone=True), nullable=True
    )
    error_details: Mapped[str | None] = mapped_column(Text, nullable=True)
