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

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

import java.time.LocalDateTime;

/**
 * 화자 엔티티
 * 음성 화자 정보를 저장하는 JPA 엔티티
 */
@Entity
@Table(name = "speakers")
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder
@ToString
public class SpeakerEntity extends BaseTimeEntity {
    
    @Id
    @Column(name = "speaker_id", length = 50)
    private String speakerId;
    
    @Column(name = "speaker_name", length = 100)
    private String speakerName;
    
    @Column(name = "profile_id", length = 100)
    private String profileId;
    
    @Column(name = "user_id", length = 50)
    private String userId;
    
    @Column(name = "total_segments")
    private Integer totalSegments;
    
    @Column(name = "total_duration")
    private Integer totalDuration;
    
    @Column(name = "average_confidence")
    private Double averageConfidence;
    
    @Column(name = "first_appeared")
    private LocalDateTime firstAppeared;
    
    @Column(name = "last_appeared")
    private LocalDateTime lastAppeared;
    
    /**
     * 도메인 객체로 변환
     */
    public Speaker toDomain() {
        return Speaker.builder()
                .speakerId(speakerId)
                .speakerName(speakerName)
                .profileId(profileId)
                .userId(userId)
                .totalSegments(totalSegments)
                .totalDuration(totalDuration)
                .averageConfidence(averageConfidence)
                .firstAppeared(firstAppeared)
                .lastAppeared(lastAppeared)
                .createdAt(getCreatedAt())
                .updatedAt(getUpdatedAt())
                .build();
    }
    
    /**
     * 도메인 객체에서 엔티티 생성
     */
    public static SpeakerEntity fromDomain(Speaker speaker) {
        return SpeakerEntity.builder()
                .speakerId(speaker.getSpeakerId())
                .speakerName(speaker.getSpeakerName())
                .profileId(speaker.getProfileId())
                .userId(speaker.getUserId())
                .totalSegments(speaker.getTotalSegments())
                .totalDuration(speaker.getTotalDuration())
                .averageConfidence(speaker.getAverageConfidence())
                .firstAppeared(speaker.getFirstAppeared())
                .lastAppeared(speaker.getLastAppeared())
                .build();
    }
    
    /**
     * 화자 정보 업데이트
     */
    public void updateSpeakerInfo(String speakerName, String userId) {
        this.speakerName = speakerName;
        this.userId = userId;
    }
    
    /**
     * 통계 정보 업데이트
     */
    public void updateStatistics(Integer totalSegments, Integer totalDuration, Double averageConfidence) {
        this.totalSegments = totalSegments;
        this.totalDuration = totalDuration;
        this.averageConfidence = averageConfidence;
        this.lastAppeared = LocalDateTime.now();
    }
}