package com.oqpo.api.web.dto.request.auth;

import com.oqpo.api.entity.oper.AuthDtlEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
public class SaveAuthRequest {
    @NotBlank
    @ApiModelProperty(value = "CRUD")
    private String viewCd;

    @NotBlank
    @ApiModelProperty(value = "권한코드")
    private String authNo;

    @NotBlank
    @ApiModelProperty(value = "권한명")
    private String authNm;

    @NotBlank
    @ApiModelProperty(value = "권한타입코드")
    private String authTpCd;

    @ApiModelProperty(value = "상위메뉴번호")
    private Integer topMenuNo;

    @NotBlank
    @ApiModelProperty(value = "사용여부")
    private String useYn;

    @ApiModelProperty(value = "등록상세리스트")
    private List<SaveAuthDtl> gridInsertData;

    @ApiModelProperty(value = "수정상세리스트")
    private List<SaveAuthDtl> gridUpdateData;

    @ApiModelProperty(value = "삭제상세리스트")
    private List<SaveAuthDtl> gridDeleteData;

    public List<AuthDtlEntity> toAuthEntities(List<SaveAuthDtl> gridData) {
        List<AuthDtlEntity> entities = new ArrayList<>();
        gridData.forEach(x -> {
            AuthDtlEntity entity = new AuthDtlEntity();
            entity.setAuthNo(x.getAuthNo());
            entity.setMenuNo(x.getMenuNo());
            entity.setReadYn(x.getWriteYn());
            entity.setWriteYn(x.getWriteYn());
            entities.add(entity);
        });
        return entities;
    }

    @Getter
    @Builder
    @AllArgsConstructor(access = AccessLevel.PROTECTED)
    static class SaveAuthDtl {

        @NotBlank
        @ApiModelProperty(value = "권한번호")
        private String authNo;

        @NotBlank
        @ApiModelProperty(value = "메뉴번호")
        private Integer menuNo;

        @NotBlank
        @ApiModelProperty(value = "읽기여부")
        private String readYn;

        @NotBlank
        @ApiModelProperty(value = "쓰기여부")
        private String writeYn;
    }
}
