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

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

import java.time.LocalDateTime;

/**
 * 세션 응답 DTO
 */
@Getter
@Builder
@Schema(description = "세션 응답")
public class SessionResponse {

    @Schema(description = "세션 ID", example = "session-001")
    private final String sessionId;

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

    @Schema(description = "회의록 ID", example = "minutes-001")
    private final String minutesId;

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

    @Schema(description = "WebSocket URL", example = "ws://localhost:8080/ws/collaboration")
    private final String websocketUrl;

    @Schema(description = "세션 토큰", example = "eyJhbGciOiJIUzI1NiJ9...")
    private final String sessionToken;

    @Schema(description = "세션 시작 시간", example = "2025-01-25T14:00:00")
    private final LocalDateTime startedAt;

    @Schema(description = "세션 만료 시간", example = "2025-01-25T18:00:00")
    private final LocalDateTime expiresAt;

    /**
     * Meeting 객체로부터 SessionResponse 생성
     */
    public static SessionResponse from(Meeting meeting) {
        return SessionResponse.builder()
                .sessionId("session-" + meeting.getMeetingId())
                .meetingId(meeting.getMeetingId())
                .minutesId(null) // 실제로는 세션에서 가져와야 함
                .status(meeting.getStatus())
                .websocketUrl("ws://localhost:8080/ws/collaboration")
                .sessionToken("session-token-" + System.currentTimeMillis())
                .startedAt(meeting.getStartedAt() != null ? meeting.getStartedAt() : LocalDateTime.now())
                .expiresAt(LocalDateTime.now().plusHours(4))
                .build();
    }

    /**
     * Session 객체로부터 SessionResponse 생성
     */
    public static SessionResponse from(com.unicorn.hgzero.meeting.biz.domain.Session session, String websocketUrl) {
        return SessionResponse.builder()
                .sessionId(session.getSessionId())
                .meetingId(session.getMeetingId())
                .minutesId(session.getMinutesId())
                .status(session.getStatus())
                .websocketUrl(websocketUrl != null ? websocketUrl : "ws://localhost:8080/ws/collaboration")
                .sessionToken("session-token-" + System.currentTimeMillis())
                .startedAt(session.getStartedAt())
                .expiresAt(session.getStartedAt().plusHours(4))
                .build();
    }
}