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.ArrayList;
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 = "purpose", length = 500)
    private String purpose;

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

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

    @Column(name = "end_time")
    private LocalDateTime endTime;

    @Column(name = "location", length = 200)
    private String location;

    @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 = "template_id", length = 50)
    private String templateId;

    /**
     * 회의 참석자 목록 (일대다 관계)
     */
    @OneToMany(mappedBy = "meeting", cascade = CascadeType.ALL, orphanRemoval = false)
    @Builder.Default
    private List<MeetingParticipantEntity> participants = new ArrayList<>();

    public Meeting toDomain() {
        return Meeting.builder()
                .meetingId(this.meetingId)
                .title(this.title)
                .purpose(this.purpose)
                .description(this.description)
                .scheduledAt(this.scheduledAt)
                .endTime(this.endTime)
                .location(this.location)
                .startedAt(this.startedAt)
                .endedAt(this.endedAt)
                .status(this.status)
                .organizerId(this.organizerId)
                .participants(this.participants != null
                        ? this.participants.stream()
                                .map(MeetingParticipantEntity::getUserId)
                                .collect(Collectors.toList())
                        : List.of())
                .templateId(this.templateId)
                .build();
    }

    public static MeetingEntity fromDomain(Meeting meeting) {
        return MeetingEntity.builder()
                .meetingId(meeting.getMeetingId())
                .title(meeting.getTitle())
                .purpose(meeting.getPurpose())
                .description(meeting.getDescription())
                .scheduledAt(meeting.getScheduledAt())
                .endTime(meeting.getEndTime())
                .location(meeting.getLocation())
                .startedAt(meeting.getStartedAt())
                .endedAt(meeting.getEndedAt())
                .status(meeting.getStatus())
                .organizerId(meeting.getOrganizerId())
                .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();
    }
}
