package com.yimeng.okr.dto;

import com.yimeng.okr.entity.KrKeyResult;
import com.yimeng.okr.entity.KrProgressLog;
import com.yimeng.okr.entity.OkrObjective;
import lombok.Data;

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

@Data
public class OkrDetailDto {
    private Long id;
    private Long periodId;
    private Long userId;
    private String userName;
    private Long deptId;
    private String deptName;
    private String title;
    private String status;
    private Integer version;
    private List<KrWithProgress> keyResults;
    private String alignedObjectiveIds;
    private List<OkrDetailDto> alignedObjectives;
    private Long reviewedBy;
    private String reviewerName;
    private LocalDateTime reviewedAt;
    private LocalDateTime createdAt;

    private String level;
    private Long parentObjectiveId;
    private String parentObjectiveTitle;
    private Integer sortOrder;
    private Double progress;
    private List<OkrDetailDto> children;

    @Data
    public static class KrWithProgress {
        private Long id;
        private String title;
        private Double targetValue;
        private Double currentValue;
        private String status;
        private Integer weight;
        private Long assignedTo;
        private String assignedToName;
        private Long sourceKrId;
        private String sourceKrTitle;
        private String sourceKrUserName;
        private List<KrProgressLog> progressLogs;
    }

    public static OkrDetailDto from(OkrObjective obj, List<KrWithProgress> krs) {
        OkrDetailDto dto = new OkrDetailDto();
        dto.setId(obj.getId());
        dto.setPeriodId(obj.getPeriodId());
        dto.setUserId(obj.getUserId());
        dto.setDeptId(obj.getDeptId());
        dto.setTitle(obj.getTitle());
        dto.setStatus(obj.getStatus());
        dto.setVersion(obj.getVersion());
        dto.setAlignedObjectiveIds(obj.getAlignedObjectiveIds());
        dto.setReviewedBy(obj.getReviewedBy());
        dto.setReviewedAt(obj.getReviewedAt());
        dto.setCreatedAt(obj.getCreatedAt());
        dto.setLevel(obj.getLevel());
        dto.setParentObjectiveId(obj.getParentObjectiveId());
        dto.setSortOrder(obj.getSortOrder());
        dto.setProgress(obj.getProgress());
        dto.setKeyResults(krs);
        return dto;
    }
}
