package com.its.op.dto.its;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.its.op.entity.its.oper.TbUserCnncHs;
import com.its.op.entity.its.oper.TbUserInfr;
import com.its.utils.ItsUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import javax.validation.constraints.Size;
import java.io.Serializable;

/**
 * 로그인/아웃 요청 DTO Class
 */
@Data
@Builder
@ApiModel("LoginDto(로그인 정보)")
public class LoginDto implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("사용자ID")  // N VARCHAR(20)
    @JsonProperty("user_id")
    private String userId;

    @ApiModelProperty("로그인시각")
    @JsonProperty("login_hms")
    private String loginHms;

    @ApiModelProperty("로그인 결과(success, fail")
    @JsonProperty("login_result")
    private String loginResult;

    @ApiModelProperty("권한")
    @JsonProperty("role")
    private String role;

    @JsonIgnore
    private String password;

    @ApiModel("LoginReqDto(로그인 요청)")
    @Getter
    @Setter
    @ToString
    @NoArgsConstructor//(access = AccessLevel.PROTECTED)
    public static class LoginReqDto {

        @ApiModelProperty("사용자 ID")
        @JsonProperty("username")
        @Size(min=1, max=20)
        private String username;

        @ApiModelProperty("비밀번호")
        @JsonProperty("password")
        @Size(min=1, max=64)
        private String password;

        @Builder
        public LoginReqDto(String user_id, String password) {
            this.username = user_id;
            this.password = password;
        }

        public TbUserInfr toEntity() {
            return TbUserInfr.builder()
                    .userId(this.username)
                    .pwd(this.password)
                    .build();
        }

        public TbUserCnncHs toCnnsHsEntity() {
            return TbUserCnncHs.builder()
                    .userId(this.username)
                    .loginHms(ItsUtils.getSysTime())
                    .logoutHms("")
                    .build();
        }
    }

    @ApiModel("LogoutReqDto(로그아웃 요청)")
    @Getter
    @Setter
    @ToString
    @NoArgsConstructor//(access = AccessLevel.PROTECTED)
    public static class LogoutReqDto {

        @ApiModelProperty("사용자ID, Nullable = N, VARCHAR(20)")  // N VARCHAR(20)
        @JsonProperty("user_id")
        @Size(min=1, max=20)
        private String userId;

        @ApiModelProperty("로그인시각")  // N VARCHAR(14)
        @JsonProperty("login_hms")
        private String loginHms;

        @Builder
        public LogoutReqDto(String user_id, String login_hms) {
            this.userId = user_id;
            this.loginHms = login_hms;
        }

        public TbUserCnncHs toCnncHsEntity() {
            return TbUserCnncHs.builder()
                    .userId(this.userId)
                    .loginHms(this.loginHms)
                    .logoutHms(ItsUtils.getSysTime())
                    .build();
        }

    }

}
