# /opt/docker/dev/service_finder/backend/app/models/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": "data"}

    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. """
    __tablename__ = "service_staging"
    __table_args__ = {"schema": "data"}

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    name: Mapped[str] = mapped_column(String(255), index=True)
    source: Mapped[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)
    
    city: Mapped[str] = mapped_column(String(100), index=True)
    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))
    
    raw_data: Mapped[dict] = mapped_column(JSONB, server_default=text("'{}'::jsonb"))
    status: Mapped[str] = mapped_column(String(20), default="pending", index=True)
    trust_score: Mapped[int] = mapped_column(Integer, default=30)
    
    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": "data"}

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    city: Mapped[str] = mapped_column(String(100), unique=True, index=True)
    country_code: Mapped[str] = mapped_column(String(5), server_default=text("'HU'"))
    is_active: Mapped[bool] = mapped_column(Boolean, default=True)
    last_run_at: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True))