package com.unicorn.hgzero.meeting.biz.dto;

import com.unicorn.hgzero.meeting.biz.domain.Dashboard;
import lombok.Builder;
import lombok.Getter;

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

/**
 * 대시보드 데이터 전송 객체
 * 사용자별 맞춤 대시보드 정보
 */
@Getter
@Builder
public class DashboardDTO {

    /**
     * 예정된 회의 목록
     */
    private final List<UpcomingMeetingDTO> upcomingMeetings;

    /**
     * 진행 중 Todo 목록
     */
    private final List<ActiveTodoDTO> activeTodos;

    /**
     * 최근 회의록 목록
     */
    private final List<RecentMinutesDTO> myMinutes;

    /**
     * 통계 정보
     */
    private final StatisticsDTO statistics;

    /**
     * 예정된 회의 정보
     */
    @Getter
    @Builder
    public static class UpcomingMeetingDTO {
        private final String meetingId;
        private final String title;
        private final LocalDateTime startTime;
        private final LocalDateTime endTime;
        private final String location;
        private final Integer participantCount;
        private final String status;
        private final String userRole; // CREATOR, PARTICIPANT
    }

    /**
     * 진행 중 Todo 정보
     */
    @Getter
    @Builder
    public static class ActiveTodoDTO {
        private final String todoId;
        private final String content;
        private final String dueDate;
        private final String priority;
        private final String status;
        private final String minutesId;
    }

    /**
     * 최근 회의록 정보
     */
    @Getter
    @Builder
    public static class RecentMinutesDTO {
        private final String minutesId;
        private final String title;
        private final LocalDateTime meetingDate;
        private final String status;
        private final Integer participantCount;
        private final LocalDateTime lastModified;
        private final String userRole; // CREATOR, PARTICIPANT
    }

    /**
     * 통계 정보
     */
    @Getter
    @Builder
    public static class StatisticsDTO {
        private final Integer upcomingMeetingsCount;
        private final Integer activeTodosCount;
        private final Integer draftMinutesCount;
    }

    /**
     * Dashboard 도메인 객체로부터 DashboardDTO 생성
     */
    public static DashboardDTO from(Dashboard dashboard) {
        String currentUserId = dashboard.getUserId();
        
        return DashboardDTO.builder()
                .upcomingMeetings(dashboard.getUpcomingMeetings().stream()
                        .map(meeting -> UpcomingMeetingDTO.builder()
                                .meetingId(meeting.getMeetingId())
                                .title(meeting.getTitle())
                                .startTime(meeting.getScheduledAt())
                                .endTime(null) // Meeting 도메인에 endTime이 없음
                                .location(null) // Meeting 도메인에 location이 없음
                                .participantCount(meeting.getParticipants() != null ? meeting.getParticipants().size() : 0)
                                .status(meeting.getStatus())
                                .userRole(currentUserId.equals(meeting.getOrganizerId()) ? "CREATOR" : "PARTICIPANT")
                                .build())
                        .toList())
                .activeTodos(dashboard.getAssignedTodos().stream()
                        .filter(todo -> "PENDING".equals(todo.getStatus()) || "IN_PROGRESS".equals(todo.getStatus()))
                        .map(todo -> ActiveTodoDTO.builder()
                                .todoId(todo.getTodoId())
                                .content(todo.getDescription())
                                .dueDate(todo.getDueDate() != null ? todo.getDueDate().toString() : null)
                                .priority(todo.getPriority())
                                .status(todo.getStatus())
                                .minutesId(todo.getMinutesId())
                                .build())
                        .toList())
                .myMinutes(dashboard.getRecentMinutes().stream()
                        .map(minutes -> RecentMinutesDTO.builder()
                                .minutesId(minutes.getMinutesId())
                                .title(minutes.getTitle())
                                .meetingDate(minutes.getCreatedAt()) // Minutes에 meetingDate가 없어서 createdAt 사용
                                .status(minutes.getStatus())
                                .participantCount(0) // Minutes 도메인에 participantCount가 없음
                                .lastModified(minutes.getLastModifiedAt())
                                .userRole(currentUserId.equals(minutes.getCreatedBy()) ? "CREATOR" : "PARTICIPANT")
                                .build())
                        .toList())
                .statistics(StatisticsDTO.builder()
                        .upcomingMeetingsCount(dashboard.getUpcomingMeetings().size())
                        .activeTodosCount(dashboard.getStatistics().getPendingTodos())
                        .draftMinutesCount(dashboard.getStatistics().getDraftMinutes())
                        .build())
                .build();
    }
}