from datetime import datetime
from typing import Any

from sqlalchemy import BigInteger, Boolean, DateTime, ForeignKey, JSON, String, Text
from sqlalchemy.orm import Mapped, mapped_column

from app.models.base import BaseModel


class UserVerificationRecord(BaseModel):
    __tablename__ = "user_verification_records"

    user_id: Mapped[int] = mapped_column(BigInteger, ForeignKey("users.id", ondelete="CASCADE"), index=True, nullable=False)
    real_name: Mapped[str | None] = mapped_column(String(255), nullable=True)
    id_card_no: Mapped[str | None] = mapped_column(String(255), nullable=True)
    id_card_no_hash: Mapped[str | None] = mapped_column(String(64), index=True, nullable=True)
    id_card_front_image: Mapped[str | None] = mapped_column(String(500), nullable=True)
    id_card_back_image: Mapped[str | None] = mapped_column(String(500), nullable=True)
    id_card_face_image: Mapped[str | None] = mapped_column(String(500), nullable=True)
    provider: Mapped[str] = mapped_column(String(50), default="baidu", nullable=False)
    mode: Mapped[str] = mapped_column(String(32), default="two_factor", nullable=False)
    provider_task_id: Mapped[str | None] = mapped_column(String(128), index=True, nullable=True)
    status: Mapped[str] = mapped_column(String(20), default="pending", nullable=False)
    provider_response: Mapped[dict[str, Any] | None] = mapped_column(JSON, nullable=True)
    material_expires_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
    material_cleaned: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
    verified_at: Mapped[datetime | None] = mapped_column(nullable=True)
    rejected_at: Mapped[datetime | None] = mapped_column(nullable=True)
    reject_reason: Mapped[str | None] = mapped_column(Text, nullable=True)
