package com.arms.api.report.weekly.model.vo;

import lombok.Builder;
import lombok.Getter;

@Builder
@Getter
public class PerformanceComparisonVO {

    // 완료율(%) (개인)
    private String personalResolvedPercentage;
    private String personalNotResolvedPercentage;
    // 완료율(%) (평균)
    private String averageResolvedPercentage;
    private String averageNotResolvedPercentage;
    // 완료율 편차
    private String reqResolvedDifference;
    private String notReqResolvedDifference;

    // 개인 이슈 (요구사항)
    private Long reqResolved;
    private Long reqUnresolved;
    // 개인 이슈 (하위 및 연결이슈)
    private Long notReqResolved;
    private Long notReqUnresolved;

    // 총 이슈 편차
    private String reqDifference;
    private String notReqDifference;

    private Integer activeUsers;
    private Long activeUsersReqResolved;
    private Long activeUsersReqUnresolved;
    private Long activeUsersNotReqResolved;
    private Long activeUsersNotReqUnresolved;

    // 평균 이슈 (요구사항)
    private Double avgReqResolved;
    private Double avgReqUnResolved;
    // 평균 이슈 (하위 및 연결이슈)
    private Double avgNotReqResolved;
    private Double avgNotReqUnResolved;

    public static PerformanceComparisonVO from(
            long reqResolved, long reqUnresolved,
            long notReqResolved, long notReqUnresolved,
            long activeUsersReqResolved, long activeUsersReqUnresolved,
            long activeUsersNotReqResolved, long activeUsersNotReqUnresolved,
            int activeUsersCount) {

        double avgReqResolved = roundToThreeDecimals(activeUsersCount > 0 ? (double) activeUsersReqResolved / activeUsersCount : 0.0);
        double avgReqUnResolved = roundToThreeDecimals(activeUsersCount > 0 ? (double) activeUsersReqUnresolved / activeUsersCount : 0.0);
        double avgNotReqResolved = roundToThreeDecimals(activeUsersCount > 0 ? (double) activeUsersNotReqResolved / activeUsersCount : 0.0);
        double avgNotReqUnResolved = roundToThreeDecimals(activeUsersCount > 0 ? (double) activeUsersNotReqUnresolved / activeUsersCount : 0.0);

        long personalReqTotal = reqResolved + reqUnresolved;
        long personalNotReqTotal = notReqResolved + notReqUnresolved;
        double avgReqTotal = avgReqResolved + avgReqUnResolved;
        double avgNotReqTotal = avgNotReqResolved + avgNotReqUnResolved;

        return PerformanceComparisonVO.builder()
                .personalResolvedPercentage(formatPercentage(reqResolved, personalReqTotal))
                .personalNotResolvedPercentage(formatPercentage(notReqResolved, personalNotReqTotal))
                .averageResolvedPercentage(formatPercentage(avgReqResolved, avgReqTotal))
                .averageNotResolvedPercentage(formatPercentage(avgNotReqResolved, avgNotReqTotal))
                .reqResolvedDifference(formatDifference(reqResolved, avgReqResolved))
                .notReqResolvedDifference(formatDifference(notReqResolved, avgNotReqResolved))
                .reqResolved(reqResolved)
                .reqUnresolved(reqUnresolved)
                .notReqResolved(notReqResolved)
                .notReqUnresolved(notReqUnresolved)
                .reqDifference(formatDifference(personalReqTotal, avgReqTotal))
                .notReqDifference(formatDifference(personalNotReqTotal, avgNotReqTotal))
                .activeUsers(activeUsersCount)
                .activeUsersReqResolved(activeUsersReqResolved)
                .activeUsersReqUnresolved(activeUsersReqUnresolved)
                .activeUsersNotReqResolved(activeUsersNotReqResolved)
                .activeUsersNotReqUnresolved(activeUsersNotReqUnresolved)
                .avgReqResolved(avgReqResolved)
                .avgReqUnResolved(avgReqUnResolved)
                .avgNotReqResolved(avgNotReqResolved)
                .avgNotReqUnResolved(avgNotReqUnResolved)
                .build();
    }

    private static String formatDifference(double personal, double avg) {
        if (avg == 0.0) return "0.0%";
        double diff = (personal - avg) / avg * 100;
        if (diff == 0.0) return "0.0%";
        return String.format("%+.1f%%", diff);
    }

    private static String formatPercentage(double resolved, double total) {
        if (total == 0.0) return "0.0%";
        return String.format("%.1f%%", resolved / total * 100);
    }

    private static double roundToThreeDecimals(double value) {
        return Math.round(value * 1000.0) / 1000.0;
    }
}
