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

class VehicleBrand(Base):
    __tablename__ = "vehicle_brands"
    __table_args__ = {"schema": "data"}
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), nullable=False, unique=True)
    slug = Column(String(100), unique=True)
    country_of_origin = Column(String(50))
    is_active = Column(Boolean, default=True)

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)
    social_rating_pct = Column(Integer, default=80)
    location_city = Column(String(100))
    service_type = Column(String(50))
    search_tags = Column(String)
    latitude = Column(Numeric(10, 8))
    longitude = Column(Numeric(11, 8))
    is_active = Column(Boolean, default=True)
    records = relationship("ServiceRecord", back_populates="provider")

class EngineSpec(Base):
    __tablename__ = "engine_specs"
    __table_args__ = {"schema": "data"}
    id = Column(Integer, primary_key=True, index=True)
    engine_code = Column(String(50), unique=True)
    fuel_type = Column(String(20))
    power_kw = Column(Integer)
    default_service_interval_km = Column(Integer, default=15000)
    vehicles = relationship("Vehicle", back_populates="engine_spec")

class Vehicle(Base):
    __tablename__ = "vehicles"
    __table_args__ = {"schema": "data"}
    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    current_company_id = Column(Integer, ForeignKey("data.organizations.id"))
    brand_id = Column(Integer, ForeignKey("data.vehicle_brands.id"))
    model_name = Column(String(100))
    engine_spec_id = Column(Integer, ForeignKey("data.engine_specs.id"))
    identification_number = Column(String(50), unique=True)
    license_plate = Column(String(20))
    tracking_mode = Column(String(10), default="km")
    current_rating_pct = Column(Integer, default=100)
    total_real_usage = Column(Numeric(15, 2), default=0)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    
    engine_spec = relationship("EngineSpec", back_populates="vehicles")
    service_records = relationship("ServiceRecord", back_populates="vehicle", cascade="all, delete-orphan")
    current_org = relationship("Organization", back_populates="vehicles")

class ServiceRecord(Base):
    __tablename__ = "service_records"
    __table_args__ = {"schema": "data"}
    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    vehicle_id = Column(UUID(as_uuid=True), ForeignKey("data.vehicles.id"))
    provider_id = Column(Integer, ForeignKey("data.service_providers.id"))
    service_date = Column(Date, nullable=False)
    usage_value = Column(Numeric(15, 2))
    repair_quality_pct = Column(Integer, default=100)

    vehicle = relationship("Vehicle", back_populates="service_records")
    provider = relationship("ServiceProvider", back_populates="records")

class OrganizationMember(Base):
    __tablename__ = "organization_members"
    __table_args__ = {"schema": "data"}
    id = Column(Integer, primary_key=True, index=True)
    organization_id = Column(Integer, ForeignKey("data.organizations.id"), nullable=False)
    user_id = Column(Integer, ForeignKey("data.users.id"), nullable=False)
    role = Column(String, default="driver") 

    organization = relationship("Organization", back_populates="members")

# --- KOMPATIBILITÁSI RÉTEG ---
UserVehicle = Vehicle 
VehicleOwnership = Vehicle