package com.kospo.talk.model;

import com.kospo.talk.payload.QuestionDto;
import jakarta.persistence.*;
import jakarta.validation.constraints.Size;
import lombok.*;
import org.hibernate.annotations.ColumnDefault;

import java.time.LocalDateTime;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
@Table(name = "question")
public class Question {
    @EmbeddedId
    private QuestionId id;

    @Size(max = 200)
    @Column(name = "content", length = 200)
    private String content;

    @Column(name = "ask", length = Integer.MAX_VALUE)
    private String ask;

    @Size(max = 10)
    @Column(name = "ins_sabun", length = 10)
    private String insSabun;

    @Size(max = 20)
    @Column(name = "ins_name", length = 20)
    private String insName;

    @ColumnDefault("CURRENT_TIMESTAMP")
    @Column(name = "ins_date")
    private LocalDateTime insDate;

    public static Question to(QuestionDto questionDto) {
        return Question.builder()
                .content(questionDto.getContent())
                .ask(questionDto.getAsk())
                .insSabun(questionDto.getInsSabun())
                .insDate(questionDto.getInsDate())
                .insName(questionDto.getInsName())
                .id(
                        QuestionId.builder()
                                .workId(questionDto.getWorkId())
                                .sort(questionDto.getSort()).build()
                ).build();
    }
}