# /opt/docker/dev/service_finder/backend/app/models/marketplace/staged_data.py
from datetime import datetime
from typing import Optional, Any
from sqlalchemy import String, Integer, DateTime, text, Boolean, Float
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.sql import func
from app.db.base_class import Base

class StagedVehicleData(Base):
    """ Robot 2.1 (Researcher) nyers adatgyűjtője. """
    __tablename__ = "staged_vehicle_data"
    __table_args__ = {"schema": "system"}

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    source_url: Mapped[Optional[str]] = mapped_column(String)
    raw_data: Mapped[dict] = mapped_column(JSONB, server_default=text("'{}'::jsonb"))
    
    status: Mapped[str] = mapped_column(String(20), default="PENDING", index=True) 
    error_log: Mapped[Optional[str]] = mapped_column(String)
    
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())

class ServiceStaging(Base):
    """ Robot 1.3 (Scout) által talált nyers szerviz adatok és a Robot 5 (Auditor) naplója. """
    __tablename__ = "service_staging"
    __table_args__ = {"schema": "marketplace"}

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    name: Mapped[str] = mapped_column(String(255), index=True)
    source: Mapped[Optional[str]] = mapped_column(String(50))
    external_id: Mapped[Optional[str]] = mapped_column(String(100), index=True)
    fingerprint: Mapped[str] = mapped_column(String(64), unique=True, index=True)
    
    # Elérhetőségek
    city: Mapped[str] = mapped_column(String(100), index=True)
    postal_code: Mapped[Optional[str]] = mapped_column(String(10))
    full_address: Mapped[Optional[str]] = mapped_column(String(500))
    contact_phone: Mapped[Optional[str]] = mapped_column(String(50))
    website: Mapped[Optional[str]] = mapped_column(String(255))
    contact_email: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
    
    # Beküldés és Bizalom
    description: Mapped[Optional[str]] = mapped_column(Text)
    submitted_by: Mapped[Optional[int]] = mapped_column(Integer, ForeignKey("identity.users.id"))
    trust_score: Mapped[int] = mapped_column(Integer, default=0, server_default=text("0"))
    
    # Nyers adatok és Státusz
    raw_data: Mapped[dict] = mapped_column(JSONB, server_default=text("'{}'::jsonb"))
    status: Mapped[str] = mapped_column(String(20), default="pending", index=True)
    
    # --- Robot 5 (Auditor) technikai mezők ---
    # Ezek kellenek a munka naplózásához
    rejection_reason: Mapped[Optional[str]] = mapped_column(String(500))
    published_at: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True))
    service_profile_id: Mapped[Optional[int]] = mapped_column(Integer)
    organization_id: Mapped[Optional[int]] = mapped_column(Integer)
    audit_trail: Mapped[Optional[dict]] = mapped_column(JSONB)

    # Időbélyegek
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
    updated_at: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True), onupdate=func.now())

class DiscoveryParameter(Base):
    """ Felderítési paraméterek (Városok, ahol a Scout keres). """
    __tablename__ = "discovery_parameters"
    __table_args__ = {"schema": "marketplace"}

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    city: Mapped[str] = mapped_column(String(100), unique=True, index=True)
    keyword: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)
    is_active: Mapped[bool] = mapped_column(Boolean, default=True)
    last_run_at: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True))