from datetime import datetime
from typing import Optional, Any
from sqlalchemy import String, Integer, DateTime, text, Boolean, Float, Text, ForeignKey
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.sql import func
from app.database import Base # MB 2.0 Standard: Központi bázis használata

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

    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. 
    A séma és a mezők szinkronban az adatbázis audittal.
    """
    __tablename__ = "service_staging"
    __table_args__ = {"schema": "marketplace", "extend_existing": True}

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    name: Mapped[str] = mapped_column(String(255), index=True)
    
    # 1. ⚠️ EXTRA OSZLOP: source
    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)
    
    # 2. ⚠️ EXTRA OSZLOP: description
    description: Mapped[Optional[str]] = mapped_column(Text)
    
    # 3. ⚠️ EXTRA OSZLOP: submitted_by
    submitted_by: Mapped[Optional[int]] = mapped_column(Integer, ForeignKey("identity.users.id"))
    
    # 4. ⚠️ EXTRA OSZLOP: trust_score
    trust_score: Mapped[int] = mapped_column(Integer, default=0, server_default=text("0"))
    
    raw_data: Mapped[dict] = mapped_column(JSONB, server_default=text("'{}'::jsonb"))
    status: Mapped[str] = mapped_column(String(20), default="pending", index=True)
    validation_level: Mapped[int] = mapped_column(Integer, default=40, server_default=text("40"))
    
    # --- Robot 5 (Auditor) technikai mezők ---
    
    # 5. ⚠️ EXTRA OSZLOP: rejection_reason
    rejection_reason: Mapped[Optional[str]] = mapped_column(String(500))
    
    # 6. ⚠️ EXTRA OSZLOP: published_at
    published_at: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True))
    
    # 7. ⚠️ EXTRA OSZLOP: service_profile_id
    service_profile_id: Mapped[Optional[int]] = mapped_column(Integer)
    
    # 8. ⚠️ EXTRA OSZLOP: organization_id
    organization_id: Mapped[Optional[int]] = mapped_column(Integer)
    
    # 9. ⚠️ EXTRA OSZLOP: audit_trail
    audit_trail: Mapped[Optional[dict]] = mapped_column(JSONB)

    # Időbélyegek
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
    
    # 10. ⚠️ EXTRA OSZLOP: updated_at
    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", "extend_existing": True}

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    city: Mapped[str] = mapped_column(String(100), unique=True, index=True)
    country_code: Mapped[Optional[str]] = mapped_column(String(2), nullable=True, default="HU")
    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))