package com.dne.ems.dto;

import com.dne.ems.model.Feedback;
import com.dne.ems.model.enums.FeedbackStatus;
import com.dne.ems.model.enums.PollutionType;
import com.dne.ems.model.enums.SeverityLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

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

/**
 * 反馈响应数据传输对象
 * 
 * <p>该类用于封装反馈信息的响应数据，包含反馈的详细信息、状态、位置信息、
 * 提交者信息、关联任务以及附件信息等。主要用于前端展示和API响应。</p>
 *
 * @version 1.0
 * @since 2025-06
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class FeedbackResponseDTO {

    /**
     * 反馈记录的唯一标识符
     */
    private Long id;
    
    /**
     * 事件唯一标识符
     */
    private String eventId;
    
    /**
     * 反馈标题
     */
    private String title;
    
    /**
     * 反馈详细描述
     */
    private String description;
    
    /**
     * 污染类型
     */
    private PollutionType pollutionType;
    
    /**
     * 严重程度级别
     */
    private SeverityLevel severityLevel;
    
    /**
     * 反馈当前状态
     */
    private FeedbackStatus status;
    
    /**
     * 文本地址描述
     */
    private String textAddress;
    
    /**
     * 网格X坐标
     */
    private Integer gridX;
    
    /**
     * 网格Y坐标
     */
    private Integer gridY;
    
    /**
     * 地理纬度
     */
    private Double latitude;
    
    /**
     * 地理经度
     */
    private Double longitude;
    
    /**
     * 创建时间
     */
    private LocalDateTime createdAt;
    
    /**
     * 最后更新时间
     */
    private LocalDateTime updatedAt;
    
    /**
     * 提交者ID
     */
    private Long submitterId;
    
    /**
     * 提交用户信息
     */
    private UserInfoDTO user; 
    
    /**
     * 关联任务详情
     */
    private TaskDetailDTO task;
    
    /**
     * 附件列表
     */
    private List<AttachmentDTO> attachments;

    public static FeedbackResponseDTO fromEntity(Feedback feedback, TaskDetailDTO taskDetail) {
        FeedbackResponseDTO dto = new FeedbackResponseDTO();
        dto.setId(feedback.getId());
        dto.setEventId(feedback.getEventId());
        dto.setTitle(feedback.getTitle());
        dto.setDescription(feedback.getDescription());
        dto.setPollutionType(feedback.getPollutionType());
        dto.setSeverityLevel(feedback.getSeverityLevel());
        dto.setStatus(feedback.getStatus());
        dto.setTextAddress(feedback.getTextAddress());
        dto.setGridX(feedback.getGridX());
        dto.setGridY(feedback.getGridY());
        dto.setLatitude(feedback.getLatitude());
        dto.setLongitude(feedback.getLongitude());
        dto.setCreatedAt(feedback.getCreatedAt());
        dto.setUpdatedAt(feedback.getUpdatedAt());
        dto.setSubmitterId(feedback.getSubmitterId());

        if (feedback.getUser() != null) {
            dto.setUser(UserInfoDTO.fromEntity(feedback.getUser()));
        }

        if (feedback.getAttachments() != null) {
            dto.setAttachments(feedback.getAttachments().stream()
                    .map(AttachmentDTO::fromEntity)
                    .collect(Collectors.toList()));
        }

        dto.setTask(taskDetail);

        return dto;
    }
}