package io.github.js0ny.ilp_coursework.data.common;

import io.github.js0ny.ilp_coursework.data.response.DeliveryPathResponse;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

// Corresponding in Go
//

/*
 * type DroneEvent struct {
 * DroneID string `json:"drone_id"`
 * Latitude float64 `json:"latitude"`
 * Longitude float64 `json:"longitude"`
 * Timestamp string `json:"timestamp"`
 * }
 */

public record DroneEvent(String droneId, double latitude, double longitude, String timestamp) {

    static final int STEP = 1; // seconds between events

    // Helper method that converts from DeliveryPathResponse to List<DroneEvent>

    public static List<DroneEvent> fromPathResponse(DeliveryPathResponse resp) {
        List<DroneEvent> events = new java.util.ArrayList<>();
        for (var p : resp.dronePaths()) {
            String id = p.droneId() + "";
            for (var d : p.deliveries()) {
                for (var coord : d.flightPath()) {
                    String timestamp = java.time.Instant.now().toString();
                    events.add(new DroneEvent(id, coord.lat(), coord.lng(), timestamp));
                }
            }
        }
        return events;
    }

    // Helper method that converts from DeliveryPathResponse to List<DroneEvent>
    // with base timestamp
    public static List<DroneEvent> fromPathResponseWithTimestamp(
            DeliveryPathResponse resp, LocalDateTime baseTimestamp) {
        List<DroneEvent> events = new java.util.ArrayList<>();
        java.time.LocalDateTime timestamp = baseTimestamp;
        for (var p : resp.dronePaths()) {
            String id = String.valueOf(p.droneId());
            for (var d : p.deliveries()) {
                for (var coord : d.flightPath()) {
                    events.add(new DroneEvent(id, coord.lat(), coord.lng(), timestamp.toString()));
                    timestamp = timestamp.plusSeconds(STEP); // Increment timestamp for each event
                }
            }
        }
        return events;
    }

    public static List<DroneEvent> fromPathResponseWithTimestamps(
            DeliveryPathResponse resp, Map<Integer, LocalDateTime> deliveryTimestamps) {
        List<DroneEvent> events = new java.util.ArrayList<>();
        for (var p : resp.dronePaths()) {
            String id = String.valueOf(p.droneId());
            for (var d : p.deliveries()) {
                LocalDateTime timestamp = deliveryTimestamps.get(d.deliveryId());
                // Fallback to current time if the delivery does not carry a timestamp.
                System.out.println(
                        "Generated event for drone " + id + " at " + timestamp.toString());
                LocalDateTime current = timestamp != null ? timestamp : LocalDateTime.now();
                for (var coord : d.flightPath()) {
                    events.add(new DroneEvent(id, coord.lat(), coord.lng(), current.toString()));
                    current = current.plusSeconds(STEP);
                }
            }
        }
        return events;
    }
}
