package com.icomsys.main_vm.db.jpa.repo.system.dsl;

import com.icomsys.main_vm.biz.common.system.vo.SystemBotUserViewSubRes;
import com.icomsys.main_vm.biz.common.system.vo.SystemServiceGroupRes;
import com.icomsys.main_vm.common.code.enumresource.PolicyResource;
import com.icomsys.main_vm.common.code.enumresource.RoleResource;
import com.icomsys.main_vm.db.jpa.entity.system.*;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;

import java.util.List;
import java.util.stream.Collectors;

@Aspect
@Slf4j
@RequiredArgsConstructor
public class TbUserAuthGroupRepoDslImpl implements TbUserAuthGroupRepoDsl {

    private final JPAQueryFactory queryFactory;
    private final QTbUserAuthGroup qTbUserAuthGroup = QTbUserAuthGroup.tbUserAuthGroup;
    private final QTbServiceGroupPolicy qTbServiceGroupPolicy = QTbServiceGroupPolicy.tbServiceGroupPolicy;
    private final QTbServiceGrouop qTbServiceGrouop = QTbServiceGrouop.tbServiceGrouop;
    private final QTbBotUser qTbBotUser = QTbBotUser.tbBotUser;
    private final QTbUserCust qTbUserCust = QTbUserCust.tbUserCust;

    @Override
    public List<String> userPolicyListSelect(Long userSeq, String lastService) {

        TbBotUser userget = queryFactory.selectFrom(qTbBotUser).where(qTbBotUser.userSeq.eq(userSeq))
                .fetchOne();

        if (RoleResource.SYSTEMADMIN.getName().equals(userget.getRoleVal())) {
            return PolicyResource.ALL.stream().map(e -> e.getName()).collect(Collectors.toList());
//            return queryFactory.select(
//                            qTbServiceGroupPolicy.policyVal
//                    )
//                    .from(qTbUserAuthGroup)
//                    .innerJoin(qTbServiceGroupPolicy).on(qTbUserAuthGroup.authGroupSeq.eq(qTbServiceGroupPolicy.authGroupSeq))
////                .innerJoin(qTbServiceGrouop).on(qTbServiceGroupPolicy.serviceGroup.eq(qTbServiceGrouop.serviceGroup))
//                    .where(
//                            qTbUserAuthGroup.userSeq.eq(userSeq)
////                        , qTbServiceGrouop.serviceGroupUpper.eq(lastService)
//                    )
//                    .groupBy(qTbServiceGroupPolicy.policyVal)
//                    .fetch();
        } else {
            return queryFactory.select(
                            qTbServiceGroupPolicy.policyVal
                    )
                    .from(qTbServiceGrouop)
                    .innerJoin(qTbUserCust).on(qTbServiceGrouop.serviceGroup.eq(qTbUserCust.serviceGroup))
                    .innerJoin(qTbUserAuthGroup).on(qTbUserCust.serviceGroup.eq(qTbUserAuthGroup.serviceGroup))
                    .innerJoin(qTbServiceGroupPolicy).on(qTbUserAuthGroup.authGroupSeq.eq(qTbServiceGroupPolicy.authGroupSeq))
//                .innerJoin(qTbServiceGrouop).on(qTbServiceGroupPolicy.serviceGroup.eq(qTbServiceGrouop.serviceGroup))
                    .where(
                            qTbUserAuthGroup.userSeq.eq(userSeq)
                            , qTbServiceGrouop.serviceGroupUpper.eq(lastService)
                    )
                    .groupBy(qTbServiceGroupPolicy.policyVal)
                    .fetch();
        }


    }

    @Override
    public List<SystemBotUserViewSubRes> userUseAuthGroup(Long userSeq) {
        List<SystemBotUserViewSubRes> result =
                queryFactory.select(
                                Projections.fields(
                                        SystemBotUserViewSubRes.class,
                                        qTbServiceGrouop.serviceGroup,
                                        qTbServiceGrouop.serviceGroupName,
                                        qTbUserAuthGroup.authGroupSeq
                                )
                        ).from(qTbUserAuthGroup)
                        .innerJoin(qTbServiceGrouop).on(qTbServiceGrouop.serviceGroup.eq(qTbUserAuthGroup.serviceGroup))
                        .where(qTbUserAuthGroup.userSeq.eq(userSeq))
                        .fetch();

        return result;
    }

}
