from datetime import datetime, timezone

from sqlalchemy import (
    Column, Integer, String, Float, Boolean, DateTime, ForeignKey, Text, UniqueConstraint
)
from sqlalchemy.orm import relationship

from .database import Base


def utcnow():
    return datetime.now(timezone.utc)


class Truck(Base):
    __tablename__ = "trucks"

    id = Column(Integer, primary_key=True)
    geotab_device_id = Column(String, unique=True, nullable=False)
    name = Column(String, nullable=False)
    vin = Column(String, nullable=True)
    serial_number = Column(String, nullable=True)
    license_plate = Column(String, nullable=True)
    engine_model = Column(String, nullable=True)
    active = Column(Boolean, default=True)
    created_at = Column(DateTime, default=utcnow)

    # cache do ultimo dado recebido do Geotab
    odometer_miles = Column(Float, nullable=True)
    odometer_date = Column(DateTime, nullable=True)
    engine_hours = Column(Float, nullable=True)
    engine_hours_date = Column(DateTime, nullable=True)
    last_synced_at = Column(DateTime, nullable=True)

    maintenance_records = relationship("MaintenanceRecord", back_populates="truck", cascade="all, delete-orphan")
    tires = relationship("Tire", back_populates="truck", cascade="all, delete-orphan")


class MaintenanceItem(Base):
    __tablename__ = "maintenance_items"

    id = Column(Integer, primary_key=True)
    name = Column(String, nullable=False)
    interval_miles = Column(Float, nullable=True)
    interval_hours = Column(Float, nullable=True)
    interval_days = Column(Integer, nullable=True)
    warning_miles = Column(Float, default=1500)   # antecedencia p/ status "vencendo"
    warning_hours = Column(Float, default=25)
    warning_days = Column(Integer, default=15)
    active = Column(Boolean, default=True)

    maintenance_records = relationship("MaintenanceRecord", back_populates="item")


class MaintenanceRecord(Base):
    __tablename__ = "maintenance_records"

    id = Column(Integer, primary_key=True)
    truck_id = Column(Integer, ForeignKey("trucks.id"), nullable=False)
    item_id = Column(Integer, ForeignKey("maintenance_items.id"), nullable=False)
    service_date = Column(DateTime, nullable=False)
    mileage_at_service = Column(Float, nullable=True)
    hours_at_service = Column(Float, nullable=True)
    parts_cost = Column(Float, default=0)
    labor_cost = Column(Float, default=0)
    downtime_days = Column(Float, default=0)
    notes = Column(Text, nullable=True)
    created_at = Column(DateTime, default=utcnow)

    truck = relationship("Truck", back_populates="maintenance_records")
    item = relationship("MaintenanceItem", back_populates="maintenance_records")


class Tire(Base):
    __tablename__ = "tires"

    id = Column(Integer, primary_key=True)
    truck_id = Column(Integer, ForeignKey("trucks.id"), nullable=False)
    position = Column(String, nullable=False)
    brand = Column(String, nullable=True)
    install_date = Column(DateTime, nullable=True)
    install_mileage = Column(Float, nullable=True)
    status = Column(String, default="ok")  # ok / troca / removido
    notes = Column(Text, nullable=True)

    truck = relationship("Truck", back_populates="tires")

    __table_args__ = (UniqueConstraint("truck_id", "position", name="uq_truck_tire_position"),)


class SyncLog(Base):
    __tablename__ = "sync_log"

    id = Column(Integer, primary_key=True)
    started_at = Column(DateTime, default=utcnow)
    finished_at = Column(DateTime, nullable=True)
    success = Column(Boolean, default=False)
    message = Column(Text, nullable=True)
    trucks_updated = Column(Integer, default=0)


class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True)
    username = Column(String, unique=True, nullable=False)
    password_hash = Column(String, nullable=False)
    salt = Column(String, nullable=False)
    created_at = Column(DateTime, default=utcnow)
