"""search_failure_logs 테이블 ORM — 검색 실패 자동 수집 (Phase 0.3)"""

from datetime import datetime
from typing import Any

from sqlalchemy import BigInteger, Boolean, DateTime, Float, ForeignKey, Integer, String, Text
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import Mapped, mapped_column

from core.database import Base


class SearchFailureLog(Base):
    __tablename__ = "search_failure_logs"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    query: Mapped[str] = mapped_column(Text, nullable=False)
    user_id: Mapped[int | None] = mapped_column(
        BigInteger, ForeignKey("users.id", ondelete="SET NULL")
    )
    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), default=datetime.now, nullable=False
    )
    result_count: Mapped[int] = mapped_column(Integer, nullable=False)
    confidence: Mapped[float | None] = mapped_column(Float)
    failure_reason: Mapped[str] = mapped_column(String(30), nullable=False)
    context: Mapped[dict[str, Any] | None] = mapped_column(JSONB)
    reviewed: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
