from logging import Logger
from ais import AIS
from datetime import datetime, timedelta, timezone

EXPIRY_OFFSET_SECONDS = 10

from anduril import Lattice
from anduril import  (
    Aliases,
    AlternateId,
    Classification,
    ClassificationInformation,    
    Location,
    MilView,
    Ontology,
    Position,
    Provenance,
)


class AISLatticeIntegration:
    def __init__(
        self,
        logger: Logger,
        lattice: Lattice,
        ais: AIS,
    ):
        self.logger = logger
        self.lattice = lattice
        self.ais = ais

    async def publish_vessels_as_entities(self):
        """
        Asynchronously publishes vessel data as entities to the Lattice API.
        Bridges the AIS vessel data and the Lattice API

        Parameters:
            None

        Returns:
            None
        """
        for vessel_data in self.ais.get_all_data():  
            # Publishing the entity returns an object with all the data we need to maintain an internal state 
            entity = self.lattice.entities.publish_entity(
                entity_id = str(vessel_data.MMSI),
                description = "Generated by AIS Vessel Traffic Dataset",
                is_live = True,
                created_time = (datetime.now(timezone.utc)).isoformat(),
                expiry_time = (datetime.now(timezone.utc) + timedelta(seconds=EXPIRY_OFFSET_SECONDS)).isoformat(),
                aliases = Aliases(
                    name = vessel_data.VesselName,
                    alternate_ids = [
                        AlternateId(
                            id = str(vessel_data.MMSI),
                            type = "ALT_ID_TYPE_MMSI_ID"
                        )
                    ]
                ), 
                mil_view = MilView(
                    disposition = "DISPOSITION_NEUTRAL",
                    enviornment = "ENVIRONMENT_SURFACE"
                ),
                location = Location(
                    position = Position(
                        latitude_degrees = vessel_data.LAT,
                        longitude_degrees = vessel_data.LON,
                    )
                ),
                ontology = Ontology(
                    template = "TEMPLATE_TRACK",
                    # For more information, please refer to https://developer.anduril.com/guides/entities/publish#set-platform-type
                    platform_type = "Surface Vessel",
                ),
                provenance = Provenance(
                    data_type = "vessel-data",
                    integration_name =  "ais-sample-integration",
                    source_update_time = datetime.now(timezone.utc).isoformat()
                ),
                data_classification = Classification(
                    default = ClassificationInformation(
                        level = "CLASSIFICATION_LEVELS_UNCLASSIFIED"
                    )
                ) 
            )

            self.logger.debug(
                f"MMSI={vessel_data.MMSI} VESSEL NAME={vessel_data.VesselName}\n\t{entity}"
            )  