package com.eactive.eai.rms.data.entity.onl.apim.portaluser;

import com.eactive.apim.portal.portaluser.entity.PortalUser;
import com.eactive.apim.portal.portaluser.entity.PortalUserEnums;
import com.eactive.apim.portal.portaluser.entity.QPortalUser;
import com.eactive.apim.portal.portaluser.repository.PortalUserRepository;
import com.eactive.eai.rms.data.jpa.AbstractEMSDataSerivce;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.dsl.BooleanExpression;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
@Transactional(transactionManager = "transactionManagerForEMS")
public class PortalUserService extends AbstractEMSDataSerivce<PortalUser, String, PortalUserRepository> {

    public Page<PortalUser> findAll(Pageable pageable, PortalUserUISearch portalUserUISearch) {
        QPortalUser qPortalUser = QPortalUser.portalUser;

        BooleanBuilder predicate = new BooleanBuilder();

        if (StringUtils.isNotBlank(portalUserUISearch.getSearchUserStatus())) {
            predicate.and(qPortalUser.userStatus.eq(PortalUserEnums.UserStatus.valueOf(portalUserUISearch.getSearchUserStatus())));
        } else {
            predicate.and(qPortalUser.userStatus.ne(PortalUserEnums.UserStatus.REMOVED));
        }

        if (StringUtils.isNotBlank(portalUserUISearch.getSearchApprovalStatus())) {
            predicate.and(qPortalUser.approvalStatus.eq(PortalUserEnums.ApprovalStatus.valueOf(portalUserUISearch.getSearchApprovalStatus())));
        }

        if (StringUtils.isNotBlank(portalUserUISearch.getSearchUserId())) {
            predicate.and(qPortalUser.loginId.containsIgnoreCase(portalUserUISearch.getSearchUserId()));
        }

        if (StringUtils.isNotBlank(portalUserUISearch.getSearchUserName())) {
            predicate.and(qPortalUser.userName.containsIgnoreCase(portalUserUISearch.getSearchUserName()));
        }

        if (StringUtils.isNotBlank(portalUserUISearch.getSearchOrgId())) {
            predicate.and(qPortalUser.portalOrg.id.eq(portalUserUISearch.getSearchOrgId()));
        }
        if (StringUtils.isNotBlank(portalUserUISearch.getSearchMobileNumber())) {
            predicate.and(qPortalUser.mobileNumber.contains(portalUserUISearch.getSearchMobileNumber()));
        }

        if (StringUtils.isNotBlank(portalUserUISearch.getSearchRoleCode())) {
            predicate.and(qPortalUser.roleCode.eq(PortalUserEnums.RoleCode.valueOf(portalUserUISearch.getSearchRoleCode())));
        }

        return repository.findAll(predicate, pageable);
    }

    public boolean existsByLoginId(String loginId) {
        BooleanExpression predicate = QPortalUser.portalUser.loginId.eq(loginId);
        return repository.exists(predicate);
    }

    public Optional<PortalUser> findCorporateManagerByOrgId(String orgid) {
        QPortalUser qPortalUser = QPortalUser.portalUser;

        BooleanExpression predicate = qPortalUser.portalOrg.id.eq(orgid)
                .and(qPortalUser.roleCode.eq(PortalUserEnums.RoleCode.ROLE_CORP_MANAGER))
                .and(qPortalUser.userStatus.ne(PortalUserEnums.UserStatus.REMOVED));

        return Optional.ofNullable(getJPAQueryFactory()
                .selectFrom(qPortalUser)
                .where(predicate)
                .orderBy(qPortalUser.createdDate.asc())
                .fetchFirst());
    }

    public PortalUser findByUserId(String userId) {
        QPortalUser qPortalUser = QPortalUser.portalUser;

        return getJPAQueryFactory()
                .selectFrom(qPortalUser)
                .where(qPortalUser.id.eq(userId))
                .orderBy(qPortalUser.createdDate.desc())
                .fetchFirst();
    }
}
