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

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

import java.time.LocalDateTime;

/**
 * 회의 세션 Entity
 */
@Entity
@Table(name = "sessions")
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SessionEntity extends BaseTimeEntity {

    @Id
    @Column(name = "session_id", length = 50)
    private String sessionId;

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

    @Column(name = "minutes_id", length = 50)
    private String minutesId;

    @Column(name = "started_by", length = 50, nullable = false)
    private String startedBy;

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

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

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

    public Session toDomain() {
        return Session.builder()
                .sessionId(this.sessionId)
                .meetingId(this.meetingId)
                .minutesId(this.minutesId)
                .startedBy(this.startedBy)
                .startedAt(this.startedAt)
                .endedAt(this.endedAt)
                .status(this.status)
                .build();
    }

    public static SessionEntity fromDomain(Session session) {
        return SessionEntity.builder()
                .sessionId(session.getSessionId())
                .meetingId(session.getMeetingId())
                .minutesId(session.getMinutesId())
                .startedBy(session.getStartedBy())
                .startedAt(session.getStartedAt())
                .endedAt(session.getEndedAt())
                .status(session.getStatus())
                .build();
    }

    public void close() {
        this.status = "CLOSED";
        this.endedAt = LocalDateTime.now();
    }
}
