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

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

/**
 * 변환 세그먼트 엔티티
 * 음성 변환의 개별 세그먼트를 저장하는 JPA 엔티티
 */
@Entity
@Table(name = "transcript_segments")
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder
@ToString
public class TranscriptSegmentEntity extends BaseTimeEntity {
    
    @Id
    @Column(name = "segment_id", length = 50)
    private String segmentId;
    
    @Column(name = "transcript_id", length = 50)
    private String transcriptId;
    
    @Column(name = "recording_id", length = 50, nullable = false)
    private String recordingId;
    
    @Lob
    @Column(name = "text", columnDefinition = "TEXT", nullable = false)
    private String text;
    
    @Column(name = "speaker_id", length = 50)
    private String speakerId;
    
    @Column(name = "speaker_name", length = 100)
    private String speakerName;
    
    @Column(name = "timestamp", nullable = false)
    private Long timestamp;
    
    @Column(name = "duration")
    private Double duration;
    
    @Column(name = "confidence")
    private Double confidence;
    
    @Column(name = "warning_flag")
    private Boolean warningFlag;
    
    @Column(name = "chunk_index")
    private Integer chunkIndex;
    
    /**
     * 도메인 객체로 변환
     */
    public TranscriptSegment toDomain() {
        return TranscriptSegment.builder()
                .segmentId(segmentId)
                .transcriptId(transcriptId)
                .recordingId(recordingId)
                .text(text)
                .speakerId(speakerId)
                .speakerName(speakerName)
                .timestamp(timestamp)
                .duration(duration)
                .confidence(confidence)
                .warningFlag(warningFlag)
                .chunkIndex(chunkIndex)
                .createdAt(getCreatedAt())
                .build();
    }
    
    /**
     * 도메인 객체에서 엔티티 생성
     */
    public static TranscriptSegmentEntity fromDomain(TranscriptSegment segment) {
        return TranscriptSegmentEntity.builder()
                .segmentId(segment.getSegmentId())
                .transcriptId(segment.getTranscriptId())
                .recordingId(segment.getRecordingId())
                .text(segment.getText())
                .speakerId(segment.getSpeakerId())
                .speakerName(segment.getSpeakerName())
                .timestamp(segment.getTimestamp())
                .duration(segment.getDuration())
                .confidence(segment.getConfidence())
                .warningFlag(segment.getWarningFlag())
                .chunkIndex(segment.getChunkIndex())
                .build();
    }
    
    /**
     * 화자 정보 업데이트
     */
    public void updateSpeakerInfo(String speakerId, String speakerName) {
        this.speakerId = speakerId;
        this.speakerName = speakerName;
    }
}