package com.unicorn.hgzero.meeting.infra.gateway.entity;

import com.unicorn.hgzero.common.entity.BaseTimeEntity;
import com.unicorn.hgzero.meeting.biz.domain.MinutesSection;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

/**
 * 회의록 섹션 Entity
 */
@Entity
@Table(name = "minutes_sections")
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MinutesSectionEntity extends BaseTimeEntity {

    @Id
    @Column(name = "section_id", length = 50)
    private String sectionId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "minutes_id", nullable = false)
    private MinutesEntity minutes;

    @Column(name = "minutes_id", insertable = false, updatable = false)
    private String minutesId;

    @Column(name = "type", length = 50, nullable = false)
    private String type;

    @Column(name = "title", length = 200, nullable = false)
    private String title;

    @Column(name = "content", columnDefinition = "TEXT")
    private String content;

    @Column(name = "\"order\"")
    @Builder.Default
    private Integer order = 0;

    @Column(name = "verified", nullable = false)
    @Builder.Default
    private Boolean verified = false;

    @Column(name = "locked", nullable = false)
    @Builder.Default
    private Boolean locked = false;

    @Column(name = "locked_by", length = 50)
    private String lockedBy;

    public MinutesSection toDomain() {
        return MinutesSection.builder()
                .sectionId(this.sectionId)
                .minutesId(this.minutesId)
                .type(this.type)
                .title(this.title)
                .content(this.content)
                .order(this.order)
                .verified(this.verified)
                .locked(this.locked)
                .lockedBy(this.lockedBy)
                .build();
    }

    public static MinutesSectionEntity fromDomain(MinutesSection section) {
        return MinutesSectionEntity.builder()
                .sectionId(section.getSectionId())
                .minutesId(section.getMinutesId())
                .type(section.getType())
                .title(section.getTitle())
                .content(section.getContent())
                .order(section.getOrder())
                .verified(section.getVerified())
                .locked(section.getLocked())
                .lockedBy(section.getLockedBy())
                .build();
    }

    public void lock(String userId) {
        this.locked = true;
        this.lockedBy = userId;
    }

    public void unlock() {
        this.locked = false;
        this.lockedBy = null;
    }

    public void verify() {
        this.verified = true;
    }
}
