package com.kt.event.content.infra.gateway.entity;

import com.kt.event.common.entity.BaseTimeEntity;
import com.kt.event.content.biz.domain.GeneratedImage;
import com.kt.event.content.biz.domain.ImageStyle;
import com.kt.event.content.biz.domain.Platform;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

/**
 * 생성된 이미지 엔티티
 * AI가 생성한 이미지 정보를 저장
 */
@Entity
@Table(name = "generated_images", indexes = {
        @Index(name = "idx_event_draft_id", columnList = "event_draft_id"),
        @Index(name = "idx_style_platform", columnList = "style,platform")
})
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class GeneratedImageEntity extends BaseTimeEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 콘텐츠 (양방향 관계)
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "content_id")
    private ContentEntity content;

    /**
     * 이벤트 ID (이벤트 초안 ID)
     */
    @Column(name = "event_draft_id", nullable = false)
    private Long eventDraftId;

    /**
     * 이미지 스타일
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "style", nullable = false, length = 20)
    private ImageStyle style;

    /**
     * 플랫폼
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "platform", nullable = false, length = 20)
    private Platform platform;

    /**
     * CDN URL (Azure Blob Storage)
     */
    @Column(name = "cdn_url", nullable = false, length = 500)
    private String cdnUrl;

    /**
     * 프롬프트
     */
    @Column(name = "prompt", columnDefinition = "TEXT")
    private String prompt;

    /**
     * 선택 여부
     */
    @Column(name = "selected", nullable = false)
    private boolean selected;

    /**
     * 정적 팩토리 메서드: 새 이미지 생성
     *
     * @param eventDraftId 이벤트 초안 ID
     * @param style        이미지 스타일
     * @param platform     플랫폼
     * @param cdnUrl       CDN URL
     * @param prompt       프롬프트
     * @return GeneratedImageEntity
     */
    public static GeneratedImageEntity create(Long eventDraftId, ImageStyle style, Platform platform,
                                               String cdnUrl, String prompt) {
        GeneratedImageEntity entity = new GeneratedImageEntity();
        entity.eventDraftId = eventDraftId;
        entity.style = style;
        entity.platform = platform;
        entity.cdnUrl = cdnUrl;
        entity.prompt = prompt;
        entity.selected = false;
        return entity;
    }

    /**
     * 도메인 모델로 변환
     *
     * @return GeneratedImage 도메인 모델
     */
    public GeneratedImage toDomain() {
        return GeneratedImage.builder()
                .id(id)
                .eventDraftId(eventDraftId)
                .style(style)
                .platform(platform)
                .cdnUrl(cdnUrl)
                .prompt(prompt)
                .selected(selected)
                .createdAt(getCreatedAt())
                .updatedAt(getUpdatedAt())
                .build();
    }

    /**
     * 콘텐츠 할당 (양방향 관계 설정용)
     *
     * @param content 콘텐츠 엔티티
     */
    protected void assignContent(ContentEntity content) {
        this.content = content;
    }

    /**
     * 이미지 선택
     */
    public void select() {
        this.selected = true;
    }

    /**
     * 이미지 선택 해제
     */
    public void deselect() {
        this.selected = false;
    }
}
