package com.unicorn.hgzero.meeting.infra.gateway.entity;

import com.unicorn.hgzero.common.entity.BaseTimeEntity;
import com.unicorn.hgzero.meeting.biz.domain.Meeting;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

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

/**
 * 회의 Entity
 */
@Entity
@Table(name = "meetings")
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MeetingEntity extends BaseTimeEntity {

    @Id
    @Column(name = "meeting_id", length = 50)
    private String meetingId;

    @Column(name = "title", length = 200, nullable = false)
    private String title;

    @Column(name = "description", columnDefinition = "TEXT")
    private String description;

    @Column(name = "scheduled_at", nullable = false)
    private LocalDateTime scheduledAt;

    @Column(name = "started_at")
    private LocalDateTime startedAt;

    @Column(name = "ended_at")
    private LocalDateTime endedAt;

    @Column(name = "status", length = 20, nullable = false)
    @Builder.Default
    private String status = "SCHEDULED";

    @Column(name = "organizer_id", length = 50, nullable = false)
    private String organizerId;

    @Column(name = "participants", columnDefinition = "TEXT")
    private String participants;

    @Column(name = "template_id", length = 50)
    private String templateId;

    public Meeting toDomain() {
        return Meeting.builder()
                .meetingId(this.meetingId)
                .title(this.title)
                .description(this.description)
                .scheduledAt(this.scheduledAt)
                .startedAt(this.startedAt)
                .endedAt(this.endedAt)
                .status(this.status)
                .organizerId(this.organizerId)
                .participants(parseParticipants(this.participants))
                .templateId(this.templateId)
                .build();
    }

    public static MeetingEntity fromDomain(Meeting meeting) {
        return MeetingEntity.builder()
                .meetingId(meeting.getMeetingId())
                .title(meeting.getTitle())
                .description(meeting.getDescription())
                .scheduledAt(meeting.getScheduledAt())
                .startedAt(meeting.getStartedAt())
                .endedAt(meeting.getEndedAt())
                .status(meeting.getStatus())
                .organizerId(meeting.getOrganizerId())
                .participants(formatParticipants(meeting.getParticipants()))
                .templateId(meeting.getTemplateId())
                .build();
    }

    public void start() {
        this.status = "IN_PROGRESS";
        this.startedAt = LocalDateTime.now();
    }

    public void end() {
        this.status = "COMPLETED";
        this.endedAt = LocalDateTime.now();
    }

    private static List<String> parseParticipants(String participants) {
        if (participants == null || participants.isEmpty()) {
            return List.of();
        }
        return Arrays.asList(participants.split(","));
    }

    private static String formatParticipants(List<String> participants) {
        if (participants == null || participants.isEmpty()) {
            return "";
        }
        return String.join(",", participants);
    }
}
