package com.unicorn.hgzero.stt.repository.entity;

import com.unicorn.hgzero.common.entity.BaseTimeEntity;
import com.unicorn.hgzero.stt.domain.Recording;
import jakarta.persistence.*;
import lombok.*;

import java.time.LocalDateTime;

/**
 * 녹음 엔티티
 * 회의 음성 녹음 정보를 저장하는 JPA 엔티티
 */
@Entity
@Table(name = "recordings")
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder
@ToString
public class RecordingEntity extends BaseTimeEntity {
    
    @Id
    @Column(name = "recording_id", length = 50)
    private String recordingId;
    
    @Column(name = "meeting_id", length = 50, nullable = false)
    private String meetingId;
    
    @Column(name = "session_id", length = 50, nullable = false)
    private String sessionId;
    
    @Enumerated(EnumType.STRING)
    @Column(name = "status", length = 20, nullable = false)
    private Recording.RecordingStatus status;
    
    @Column(name = "start_time")
    private LocalDateTime startTime;
    
    @Column(name = "end_time")
    private LocalDateTime endTime;
    
    @Column(name = "duration")
    private Integer duration;

    @Column(name = "language", length = 10, nullable = false)
    private String language;
    
    @Column(name = "speaker_count")
    private Integer speakerCount;
    
    @Column(name = "segment_count")
    private Integer segmentCount;
    
    /**
     * 도메인 객체로 변환
     */
    public Recording toDomain() {
        return Recording.builder()
                .recordingId(recordingId)
                .meetingId(meetingId)
                .sessionId(sessionId)
                .status(status)
                .startTime(startTime)
                .endTime(endTime)
                .duration(duration)
                .language(language)
                .speakerCount(speakerCount)
                .segmentCount(segmentCount)
                .build();
    }
    
    /**
     * 도메인 객체에서 엔티티 생성
     */
    public static RecordingEntity fromDomain(Recording recording) {
        return RecordingEntity.builder()
                .recordingId(recording.getRecordingId())
                .meetingId(recording.getMeetingId())
                .sessionId(recording.getSessionId())
                .status(recording.getStatus())
                .startTime(recording.getStartTime())
                .endTime(recording.getEndTime())
                .duration(recording.getDuration())
                .language(recording.getLanguage())
                .speakerCount(recording.getSpeakerCount())
                .segmentCount(recording.getSegmentCount())
                .build();
    }
    
    /**
     * 녹음 상태 업데이트
     */
    public void updateStatus(Recording.RecordingStatus newStatus) {
        this.status = newStatus;
    }
    
    /**
     * 화자 수 업데이트
     */
    public void updateSpeakerCount(Integer speakerCount) {
        this.speakerCount = speakerCount;
    }
    
    /**
     * 세그먼트 수 업데이트
     */
    public void updateSegmentCount(Integer segmentCount) {
        this.segmentCount = segmentCount;
    }
}