from sqlalchemy import Column, String, Float, DateTime, Boolean, Integer
from sqlalchemy.sql import func
from database import Base


class Vessel(Base):
    __tablename__ = "vessels"

    mmsi        = Column(String, primary_key=True)
    nombre      = Column(String, nullable=True)
    tipo        = Column(Integer, nullable=True)   # AIS ship type code
    bandera     = Column(String, nullable=True)
    destino     = Column(String, nullable=True)

    # Posición actual
    lat         = Column(Float, nullable=True)
    lon         = Column(Float, nullable=True)
    sog         = Column(Float, nullable=True)     # speed over ground (knots)
    cog         = Column(Float, nullable=True)     # course over ground (degrees)
    heading     = Column(Float, nullable=True)
    status      = Column(Integer, nullable=True)   # AIS nav status

    ultima_senal = Column(DateTime, nullable=True)
    activo      = Column(Boolean, default=True)

class VesselTrack(Base):
    __tablename__ = "vessel_tracks"

    id          = Column(Integer, primary_key=True, autoincrement=True)
    mmsi        = Column(String, nullable=False)
    timestamp   = Column(DateTime, server_default=func.now())
    lat         = Column(Float, nullable=False)
    lon         = Column(Float, nullable=False)
    sog         = Column(Float, nullable=True)
    cog         = Column(Float, nullable=True)
    heading     = Column(Float, nullable=True)
    evento_id   = Column(String, nullable=True)   # grabación asociada

class RecordingEvent(Base):
    __tablename__ = "recording_events"

    id          = Column(String, primary_key=True)
    mmsi        = Column(String, nullable=False)
    aid_id      = Column(String, nullable=False)
    inicio      = Column(DateTime, nullable=False)
    fin         = Column(DateTime, nullable=True)
    distancia_min_m = Column(Float, nullable=True)
    trigger     = Column(String)  # PROXIMIDAD | PROYECCION | MANUAL
    cerrado     = Column(Boolean, default=False)


class AtonTrack(Base):
    """Continuous position history for AIS AtoN (Type 21) devices."""
    __tablename__ = "aton_tracks"

    id           = Column(Integer, primary_key=True, autoincrement=True)
    mmsi         = Column(String, nullable=False, index=True)
    timestamp    = Column(DateTime, server_default=func.now(), index=True)
    lat          = Column(Float, nullable=False)
    lon          = Column(Float, nullable=False)
    voltage_v    = Column(Float, nullable=True)
    off_position = Column(Boolean, nullable=True)
