from sqlalchemy import Column, Integer, String, Boolean, ForeignKey, Numeric, DateTime, JSON, Date
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
import uuid
from app.db.base import Base

# 1. GLOBÁLIS KATALÓGUS (A rendszer agya)
class VehicleCatalog(Base):
    __tablename__ = "vehicle_catalog"
    __table_args__ = {"schema": "data"}
    
    id = Column(Integer, primary_key=True, index=True)
    brand = Column(String(100), nullable=False)
    model = Column(String(100), nullable=False)
    generation = Column(String(100))
    year_from = Column(Integer)
    year_to = Column(Integer)
    category = Column(String(50)) # car, bike, boat, plane, truck
    engine_type = Column(String(50)) # petrol, diesel, electric, hybrid
    engine_power_kw = Column(Integer)
    
    # DNS ADATOK: Minden technikai részlet (kerékméret, olajmennyiség, stb.)
    factory_specs = Column(JSON, default={})
    # SZERVIZTERV: Gyári előírások
    maintenance_plan = Column(JSON, default={})
    
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())

# 2. EGYEDI ESZKÖZ (Asset) - A felhasználó tulajdona
class Asset(Base):
    __tablename__ = "assets"
    __table_args__ = {"schema": "data"}
    
    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    uai = Column(String(100), unique=True, nullable=False) # VIN, HIN vagy Serial Number
    organization_id = Column(Integer, ForeignKey("data.organizations.id"))
    catalog_id = Column(Integer, ForeignKey("data.vehicle_catalog.id"), nullable=True)
    
    asset_type = Column(String(20), nullable=False) # car, boat, plane
    name = Column(String(255)) # "Kincses E39"
    
    # Dinamikus állapot
    current_plate_number = Column(String(20))
    current_country_code = Column(String(2))
    odometer_value = Column(Numeric(12, 2), default=0)
    operating_hours = Column(Numeric(12, 2), default=0)
    
    # Egyedi DNS (Gyári config + utólagos módosítások)
    factory_config = Column(JSON, default={})
    aftermarket_mods = Column(JSON, default={})
    
    status = Column(String(50), default="active")
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())

    # Kapcsolatok
    organization = relationship("Organization", back_populates="assets")
    catalog_entry = relationship("VehicleCatalog")
    events = relationship("AssetEvent", back_populates="asset", cascade="all, delete-orphan")
    ratings = relationship("AssetRating", back_populates="asset")

# 3. DIGITÁLIS SZERVIZKÖNYV / ESEMÉNYTÁR
class AssetEvent(Base):
    __tablename__ = "asset_events"
    __table_args__ = {"schema": "data"}
    
    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    asset_id = Column(UUID(as_uuid=True), ForeignKey("data.assets.id"))
    event_type = Column(String(50)) # SERVICE, REPAIR, INSPECTION, ACCIDENT, PLATE_CHANGE
    
    odometer_at_event = Column(Numeric(12, 2))
    hours_at_event = Column(Numeric(12, 2))
    
    description = Column(String)
    cost = Column(Numeric(12, 2))
    currency = Column(String(3), default="EUR")
    
    is_verified = Column(Boolean, default=False)
    attachments = Column(JSON, default=[]) # Számlák, fotók linkjei
    
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    asset = relationship("Asset", back_populates="events")

# 4. EMOCIONÁLIS ÉRTÉKELÉS
class AssetRating(Base):
    __tablename__ = "asset_ratings"
    __table_args__ = {"schema": "data"}
    
    id = Column(Integer, primary_key=True)
    asset_id = Column(UUID(as_uuid=True), ForeignKey("data.assets.id"))
    user_id = Column(Integer, ForeignKey("data.users.id"))
    
    comfort_score = Column(Integer) # 1-10
    experience_score = Column(Integer) # 1-10
    practicality_score = Column(Integer) # 1-10
    comment = Column(String)
    
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    asset = relationship("Asset", back_populates="ratings")

# 5. SZOLGÁLTATÓK (Szerelők, Partnerek)
class ServiceProvider(Base):
    __tablename__ = "service_providers"
    __table_args__ = {"schema": "data"}
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(255), nullable=False)
    official_brand_partner = Column(Boolean, default=False)
    technical_rating_pct = Column(Integer, default=80)
    location_city = Column(String(100))
    is_active = Column(Boolean, default=True)

# --- KOMPATIBILITÁSI RÉTEG ---
Vehicle = Asset
ServiceRecord = AssetEvent