# /opt/docker/dev/service_finder/backend/app/models/system/document.py
import uuid
from datetime import datetime
from typing import Optional
from sqlalchemy import String, Integer, Boolean, DateTime, ForeignKey, Text
from sqlalchemy.dialects.postgresql import UUID as PG_UUID
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.sql import func
from app.database import Base # MB 2.0: Egységesített Base a szinkronitáshoz

class Document(Base):
    """ NAS alapú dokumentumtár metaadatai. """
    __tablename__ = "documents"
    __table_args__ = {"schema": "system"}

    id: Mapped[uuid.UUID] = mapped_column(PG_UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    parent_type: Mapped[str] = mapped_column(String(20)) # 'organization' vagy 'asset'
    parent_id: Mapped[str] = mapped_column(String(50), index=True)
    doc_type: Mapped[Optional[str]] = mapped_column(String(50))
    
    original_name: Mapped[str] = mapped_column(String(255))
    file_hash: Mapped[str] = mapped_column(String(64))
    file_ext: Mapped[str] = mapped_column(String(10), default="webp")
    mime_type: Mapped[str] = mapped_column(String(100), default="image/webp")
    file_size: Mapped[Optional[int]] = mapped_column(Integer)
    
    has_thumbnail: Mapped[bool] = mapped_column(Boolean, default=False)
    thumbnail_path: Mapped[Optional[str]] = mapped_column(String(255))
    
    uploaded_by: Mapped[Optional[int]] = mapped_column(Integer, ForeignKey("identity.users.id"))
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())

    # =========================================================================
    # THOUGHT PROCESS & ARCHITECTURE DECISIONS (2026-03-04)
    # =========================================================================
    # Probléma: Az `ocr_robot.py` (Robot 3) módosítani próbálta a dokumentumok
    # állapotát és menteni akarta az AI eredményeket, de a mezők hiányoztak.
    # =========================================================================
    
    status: Mapped[str] = mapped_column(String(50), default="uploaded", index=True)
    ocr_data: Mapped[Optional[str]] = mapped_column(Text)
    error_log: Mapped[Optional[str]] = mapped_column(Text)