package incheon.ags.ias.srvy.srvyQitem.web.dto;

import incheon.ags.ias.srvy.srvyQitem.vo.SrvyQitemVO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;

@Getter
@Setter
public class SrvyQitemRequestDTO {

    @Schema(description = "설문조사 일련번호", example = "1", required = true)
    @NotNull(message = "설문조사 일련번호는 필수 항목입니다.")
    private String srvySn;

    @Schema(description = "설문문항 일련번호", example = "1")
    private Integer qitemSn;

    @Schema(description = "설문문항명", example = "전반적인 서비스에 만족하시나요?", required = true)
    @NotBlank(message = "설문문항명은 필수 항목입니다.")
    private String qitemCn;

    @Schema(description = "설문문항 유형 코드", example = "1", required = true)
    @NotBlank(message = "설문문항 유형 코드는 필수 항목입니다.")
    private String qitemTypeCd;

    @Schema(description = "최대 선택 수", example = "1")
    private Integer maxChcCo;

    @Schema(description = "설문문항 정렬 순서", example = "1", required = true)
    @NotNull(message = "설문문항 정렬 순서는 필수 항목입니다.")
    private Integer sortSeq;

    @Schema(description = "필수 응답 여부", example = "Y", required = true)
    @NotBlank(message = "필수 응답 여부는 필수 항목입니다.")
    private String esntlYn;


    @Schema(description = "최초 등록자 ID", example = "ADMIN")
    private String frstRegId;

    @Schema(description = "최초 등록 일시", example = "2024-01-01T09:00:00")
    @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    private LocalDateTime frstRegDt;

    @Schema(description = "최종 수정자 ID", example = "ADMIN")
    private String lastMdfcnId;

    @Schema(description = "최종 수정 일시", example = "2024-01-02T10:00:00")
    @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    private LocalDateTime lastMdfcnDt;

    public SrvyQitemVO toEntity() {
        SrvyQitemVO vo = new SrvyQitemVO();
        // 문자열 srvySn을 Integer로 변환
        if (this.srvySn != null && !this.srvySn.trim().isEmpty()) {
            vo.setSrvySn(Integer.parseInt(this.srvySn));
        }
        vo.setQitemSn(this.qitemSn);
        vo.setQitemCn(this.qitemCn);
        vo.setQitemTypeCd(this.qitemTypeCd);
        
        // Integer 타입으로 직접 설정
        vo.setMaxChcCo(this.maxChcCo != null ? this.maxChcCo : 1);
        vo.setSortSeq(this.sortSeq != null ? this.sortSeq : 1);
        
        vo.setEsntlYn(this.esntlYn);
        vo.setFrstRegId(this.frstRegId);
        vo.setFrstRegDt(this.frstRegDt != null ? java.sql.Timestamp.valueOf(this.frstRegDt) : null);
        vo.setLastMdfcnId(this.lastMdfcnId);
        vo.setLastMdfcnDt(this.lastMdfcnDt != null ? java.sql.Timestamp.valueOf(this.lastMdfcnDt) : null);
        return vo;
    }
}
