package com.seer.teach.mp.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.common.enums.RoleEnum;
import com.seer.teach.common.utils.AssertUtils;
import com.seer.teach.common.utils.PageConverterUtils;
import com.seer.teach.mp.admin.controller.req.AgentEmployeeRelationQueryReq;
import com.seer.teach.mp.admin.controller.req.AgentEmployeeRelationReq;
import com.seer.teach.mp.admin.controller.req.AgentEmployeeSaveReq;
import com.seer.teach.mp.admin.controller.resp.AgentEmployeeRelationResp;
import com.seer.teach.mp.admin.convert.AdminAgentEmployeeRelationConvert;
import com.seer.teach.mp.admin.service.IAdminAgentEmployeeRelationService;
import com.seer.teach.mp.entity.MpAgentEmployeeRelationEntity;
import com.seer.teach.mp.entity.MpAgentEntity;
import com.seer.teach.mp.service.IMpAgentEmployeeRelationService;
import com.seer.teach.mp.service.IMpAgentService;
import com.seer.teach.user.api.UserInfoServiceApi;
import com.seer.teach.user.api.dto.UserInfoDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 管理端代理商员工关系服务实现类
 * </p>
 *
 * @author Lingma
 * @since 2025-12-30
 */
@Service
@RequiredArgsConstructor
public class AdminAgentEmployeeRelationServiceImpl implements IAdminAgentEmployeeRelationService {

    private final IMpAgentEmployeeRelationService agentEmployeeRelationService;

    private final IMpAgentService mpAgentService;

    private final UserInfoServiceApi userInfoServiceApi;

    @Override
    public PageListBean<AgentEmployeeRelationResp> pageList(AgentEmployeeRelationQueryReq query) {
        Page<MpAgentEmployeeRelationEntity> pageParm = new Page<>(query.getPageNo(), query.getPageSize());
        var pageResult = agentEmployeeRelationService.page(pageParm, new LambdaQueryWrapper<>(MpAgentEmployeeRelationEntity.class)
                .like(Objects.nonNull(query.getAgentName()), MpAgentEmployeeRelationEntity::getAgentName, query.getAgentName())
                .like(Objects.nonNull(query.getEmployeeName()), MpAgentEmployeeRelationEntity::getEmployeeName, query.getEmployeeName())
                .eq(Objects.nonNull(query.getStatus()), MpAgentEmployeeRelationEntity::getStatus, query.getStatus()));

        return PageConverterUtils.convertPageListBean(pageResult, AdminAgentEmployeeRelationConvert.INSTANCE::convertToRespList);
    }

    @Override
    public boolean saveOrUpdateRelation(AgentEmployeeRelationReq request) {
        MpAgentEmployeeRelationEntity entity = AdminAgentEmployeeRelationConvert.INSTANCE.convert(request);
        return agentEmployeeRelationService.saveOrUpdateRelation(entity);
    }

    @Override
    public boolean deleteRelation(Integer id) {
        return agentEmployeeRelationService.deleteRelation(id);
    }

    @Override
    public AgentEmployeeRelationResp getById(Integer id) {
        MpAgentEmployeeRelationEntity entity = agentEmployeeRelationService.getById(id);
        return AdminAgentEmployeeRelationConvert.INSTANCE.convertToResp(entity);
    }

    @Override
    public List<AgentEmployeeRelationResp> getEmployeeRelationsByAgentId(Integer agentId) {
        var relations = agentEmployeeRelationService.getEmployeeRelationsByAgentId(agentId);
        return relations.stream()
                .map(AdminAgentEmployeeRelationConvert.INSTANCE::convertToResp)
                .toList();
    }

    @Override
    public boolean save(AgentEmployeeSaveReq request) {
        MpAgentEmployeeRelationEntity one = agentEmployeeRelationService.lambdaQuery()
                .eq(MpAgentEmployeeRelationEntity::getEmployeeName, request.getEmployeeName()).one();
        AssertUtils.isNull(one, ResultCodeEnum.AGENT_EMPLOYEE_ALREADY_EXISTS);
        UserInfoDTO userInfoDTO = new UserInfoDTO();
        userInfoDTO.setUserName(request.getEmployeeName());
        userInfoDTO.setPassword(request.getPassword());
        userInfoDTO.setRoleCode(RoleEnum.AGENT);
        Integer userId = userInfoServiceApi.addUserInfoAndAssignRole(userInfoDTO);
        Integer agentId = mpAgentService.lambdaQuery().eq(MpAgentEntity::getAgentName, request.getAgentName()).one().getId();
        MpAgentEmployeeRelationEntity entity = new MpAgentEmployeeRelationEntity();
        entity.setAgentId(agentId);
        entity.setEmployeeUserId(userId);
        entity.setEmployeeName(request.getEmployeeName());
        entity.setAgentName(request.getAgentName());
        entity.setPosition(request.getPosition());
        entity.setStatus(request.getStatus());
        agentEmployeeRelationService.save(entity);
        return true;
    }
}