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

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

import java.time.LocalDateTime;

/**
 * 음성 변환 엔티티
 * 음성-텍스트 변환 결과를 저장하는 JPA 엔티티
 */
@Entity
@Table(name = "transcriptions")
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder
@ToString
public class TranscriptionEntity extends BaseTimeEntity {
    
    @Id
    @Column(name = "transcript_id", length = 50)
    private String transcriptId;
    
    @Column(name = "recording_id", length = 50, nullable = false)
    private String recordingId;
    
    @Lob
    @Column(name = "full_text", columnDefinition = "TEXT")
    private String fullText;
    
    @Column(name = "segment_count")
    private Integer segmentCount;
    
    @Column(name = "total_duration")
    private Integer totalDuration;
    
    @Column(name = "average_confidence")
    private Double averageConfidence;
    
    @Column(name = "speaker_count")
    private Integer speakerCount;
    
    @Column(name = "completed_at")
    private LocalDateTime completedAt;
    
    /**
     * 도메인 객체로 변환
     */
    public Transcription toDomain() {
        return Transcription.builder()
                .transcriptId(transcriptId)
                .recordingId(recordingId)
                .fullText(fullText)
                .segmentCount(segmentCount)
                .totalDuration(totalDuration)
                .averageConfidence(averageConfidence)
                .speakerCount(speakerCount)
                .createdAt(getCreatedAt())
                .completedAt(completedAt)
                .build();
    }
    
    /**
     * 도메인 객체에서 엔티티 생성
     */
    public static TranscriptionEntity fromDomain(Transcription transcription) {
        TranscriptionEntityBuilder builder = TranscriptionEntity.builder()
                .transcriptId(transcription.getTranscriptId())
                .recordingId(transcription.getRecordingId())
                .fullText(transcription.getFullText())
                .segmentCount(transcription.getSegmentCount())
                .totalDuration(transcription.getTotalDuration())
                .averageConfidence(transcription.getAverageConfidence())
                .speakerCount(transcription.getSpeakerCount())
                .completedAt(transcription.getCompletedAt());
        
        return builder.build();
    }
    
    /**
     * 변환 완료 처리
     */
    public void complete() {
        this.completedAt = LocalDateTime.now();
    }
}