package gc.mda.kcg.domain.event;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
@JsonInclude(JsonInclude.Include.NON_NULL)
public class EventDto {

    private String id;
    private long timestamp;
    private Double lat;
    private Double lng;
    private String type;
    private String source;
    private String label;
    private String description;
    private Integer intensity;

    public static EventDto from(Event e) {
        return EventDto.builder()
                .id(e.getEventId())
                .timestamp(e.getTimestamp() != null ? e.getTimestamp().toEpochMilli() : 0)
                .lat(e.getLatitude())
                .lng(e.getLongitude())
                .type(extractType(e))
                .source(e.getSource())
                .label(e.getTitle())
                .description(e.getDescription())
                .intensity(extractIntensity(e))
                .build();
    }

    private static String extractType(Event e) {
        if (e.getRawData() != null && e.getRawData().containsKey("type")) {
            return String.valueOf(e.getRawData().get("type"));
        }
        return "alert";
    }

    private static Integer extractIntensity(Event e) {
        if (e.getRawData() != null && e.getRawData().containsKey("intensity")) {
            return ((Number) e.getRawData().get("intensity")).intValue();
        }
        return 50;
    }
}
