package gc.mda.kcg.domain.analysis;

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

import java.util.Map;

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

    private String mmsi;
    private String timestamp;
    private Classification classification;
    private Algorithms algorithms;
    private Map<String, Double> features;

    @Getter
    @Builder
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public static class Classification {
        private String vesselType;
        private Double confidence;
        private Double fishingPct;
        private Integer clusterId;
        private String season;
    }

    @Getter
    @Builder
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public static class Algorithms {
        private LocationInfo location;
        private ActivityInfo activity;
        private DarkVesselInfo darkVessel;
        private GpsSpoofingInfo gpsSpoofing;
        private ClusterInfo cluster;
        private FleetRoleInfo fleetRole;
        private RiskScoreInfo riskScore;
    }

    @Getter
    @Builder
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public static class LocationInfo {
        private String zone;
        private Double distToBaselineNm;
    }

    @Getter
    @Builder
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public static class ActivityInfo {
        private String state;
        private Double ucafScore;
        private Double ucftScore;
    }

    @Getter
    @Builder
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public static class DarkVesselInfo {
        private Boolean isDark;
        private Integer gapDurationMin;
    }

    @Getter
    @Builder
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public static class GpsSpoofingInfo {
        private Double spoofingScore;
        private Double bd09OffsetM;
        private Integer speedJumpCount;
    }

    @Getter
    @Builder
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public static class ClusterInfo {
        private Integer clusterId;
        private Integer clusterSize;
    }

    @Getter
    @Builder
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public static class FleetRoleInfo {
        private Boolean isLeader;
        private String role;
    }

    @Getter
    @Builder
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public static class RiskScoreInfo {
        private Integer score;
        private String level;
    }

    public static VesselAnalysisDto from(VesselAnalysisResult r) {
        return VesselAnalysisDto.builder()
                .mmsi(r.getMmsi())
                .timestamp(r.getTimestamp().toString())
                .classification(Classification.builder()
                        .vesselType(r.getVesselType())
                        .confidence(r.getConfidence())
                        .fishingPct(r.getFishingPct())
                        .clusterId(r.getClusterId())
                        .season(r.getSeason())
                        .build())
                .algorithms(Algorithms.builder()
                        .location(LocationInfo.builder()
                                .zone(r.getZone())
                                .distToBaselineNm(r.getDistToBaselineNm())
                                .build())
                        .activity(ActivityInfo.builder()
                                .state(r.getActivityState())
                                .ucafScore(r.getUcafScore())
                                .ucftScore(r.getUcftScore())
                                .build())
                        .darkVessel(DarkVesselInfo.builder()
                                .isDark(r.getIsDark())
                                .gapDurationMin(r.getGapDurationMin())
                                .build())
                        .gpsSpoofing(GpsSpoofingInfo.builder()
                                .spoofingScore(r.getSpoofingScore())
                                .bd09OffsetM(r.getBd09OffsetM())
                                .speedJumpCount(r.getSpeedJumpCount())
                                .build())
                        .cluster(ClusterInfo.builder()
                                .clusterId(r.getClusterId())
                                .clusterSize(r.getClusterSize())
                                .build())
                        .fleetRole(FleetRoleInfo.builder()
                                .isLeader(r.getIsLeader())
                                .role(r.getFleetRole())
                                .build())
                        .riskScore(RiskScoreInfo.builder()
                                .score(r.getRiskScore())
                                .level(r.getRiskLevel())
                                .build())
                        .build())
                .features(r.getFeatures())
                .build();
    }
}
