package com.unicorn.hgzero.meeting.infra.dto.response;

import com.unicorn.hgzero.meeting.biz.dto.DashboardDTO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
import lombok.Getter;

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

/**
 * 대시보드 응답 DTO
 */
@Getter
@Builder
@Schema(description = "대시보드 응답")
public class DashboardResponse {

    @Schema(description = "예정된 회의 목록")
    private final List<UpcomingMeetingResponse> upcomingMeetings;

    @Schema(description = "진행 중 Todo 목록")
    private final List<ActiveTodoResponse> activeTodos;

    @Schema(description = "최근 회의록 목록")
    private final List<RecentMinutesResponse> myMinutes;

    @Schema(description = "통계 정보")
    private final StatisticsResponse statistics;

    /**
     * DashboardDTO로부터 DashboardResponse 생성
     */
    public static DashboardResponse from(DashboardDTO dto) {
        return DashboardResponse.builder()
                .upcomingMeetings(dto.getUpcomingMeetings().stream()
                        .map(UpcomingMeetingResponse::from)
                        .toList())
                .activeTodos(dto.getActiveTodos().stream()
                        .map(ActiveTodoResponse::from)
                        .toList())
                .myMinutes(dto.getMyMinutes().stream()
                        .map(RecentMinutesResponse::from)
                        .toList())
                .statistics(StatisticsResponse.from(dto.getStatistics()))
                .build();
    }

    @Getter
    @Builder
    @Schema(description = "예정된 회의 정보")
    public static class UpcomingMeetingResponse {
        @Schema(description = "회의 ID", example = "550e8400-e29b-41d4-a716-446655440000")
        private final String meetingId;

        @Schema(description = "회의 제목", example = "Q1 전략 회의")
        private final String title;

        @Schema(description = "회의 시작 시간", example = "2025-01-25T14:00:00")
        private final LocalDateTime startTime;

        @Schema(description = "회의 종료 시간", example = "2025-01-25T16:00:00")
        private final LocalDateTime endTime;

        @Schema(description = "회의 장소", example = "회의실 A")
        private final String location;

        @Schema(description = "참석자 수", example = "5")
        private final Integer participantCount;

        @Schema(description = "회의 상태", example = "SCHEDULED")
        private final String status;

        public static UpcomingMeetingResponse from(DashboardDTO.UpcomingMeetingDTO dto) {
            return UpcomingMeetingResponse.builder()
                    .meetingId(dto.getMeetingId())
                    .title(dto.getTitle())
                    .startTime(dto.getStartTime())
                    .endTime(dto.getEndTime())
                    .location(dto.getLocation())
                    .participantCount(dto.getParticipantCount())
                    .status(dto.getStatus())
                    .build();
        }
    }

    @Getter
    @Builder
    @Schema(description = "진행 중 Todo 정보")
    public static class ActiveTodoResponse {
        @Schema(description = "Todo ID", example = "660e8400-e29b-41d4-a716-446655440000")
        private final String todoId;

        @Schema(description = "Todo 내용", example = "API 설계 문서 작성")
        private final String content;

        @Schema(description = "마감일", example = "2025-01-30")
        private final String dueDate;

        @Schema(description = "우선순위", example = "HIGH")
        private final String priority;

        @Schema(description = "Todo 상태", example = "IN_PROGRESS")
        private final String status;

        @Schema(description = "회의록 ID", example = "770e8400-e29b-41d4-a716-446655440000")
        private final String minutesId;

        public static ActiveTodoResponse from(DashboardDTO.ActiveTodoDTO dto) {
            return ActiveTodoResponse.builder()
                    .todoId(dto.getTodoId())
                    .content(dto.getContent())
                    .dueDate(dto.getDueDate())
                    .priority(dto.getPriority())
                    .status(dto.getStatus())
                    .minutesId(dto.getMinutesId())
                    .build();
        }
    }

    @Getter
    @Builder
    @Schema(description = "최근 회의록 정보")
    public static class RecentMinutesResponse {
        @Schema(description = "회의록 ID", example = "770e8400-e29b-41d4-a716-446655440000")
        private final String minutesId;

        @Schema(description = "회의록 제목", example = "아키텍처 설계 회의")
        private final String title;

        @Schema(description = "회의 일시", example = "2025-01-23T14:00:00")
        private final LocalDateTime meetingDate;

        @Schema(description = "회의록 상태", example = "FINALIZED")
        private final String status;

        @Schema(description = "참석자 수", example = "6")
        private final Integer participantCount;

        @Schema(description = "최종 수정 시간", example = "2025-01-23T16:30:00")
        private final LocalDateTime lastModified;

        public static RecentMinutesResponse from(DashboardDTO.RecentMinutesDTO dto) {
            return RecentMinutesResponse.builder()
                    .minutesId(dto.getMinutesId())
                    .title(dto.getTitle())
                    .meetingDate(dto.getMeetingDate())
                    .status(dto.getStatus())
                    .participantCount(dto.getParticipantCount())
                    .lastModified(dto.getLastModified())
                    .build();
        }
    }

    @Getter
    @Builder
    @Schema(description = "통계 정보")
    public static class StatisticsResponse {
        @Schema(description = "예정된 회의 수", example = "2")
        private final Integer upcomingMeetingsCount;

        @Schema(description = "진행 중 Todo 수", example = "5")
        private final Integer activeTodosCount;

        @Schema(description = "Todo 완료율", example = "68.5")
        private final Double todoCompletionRate;

        public static StatisticsResponse from(DashboardDTO.StatisticsDTO dto) {
            return StatisticsResponse.builder()
                    .upcomingMeetingsCount(dto.getUpcomingMeetingsCount())
                    .activeTodosCount(dto.getActiveTodosCount())
                    .todoCompletionRate(dto.getTodoCompletionRate())
                    .build();
        }
    }
}