package cd.casic.module.machine.service.impl;

import cd.casic.module.machine.controller.vo.MachineEnvVO;
import cd.casic.module.machine.dal.dataobject.MachineEnvDO;
import cd.casic.module.machine.dal.mysql.MachineEnvMapper;
import cd.casic.module.machine.service.MachineEnvService;
import cd.casic.framework.commons.pojo.PageResult;
import com.google.common.annotations.VisibleForTesting;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cd.casic.framework.commons.util.object.BeanUtils;

import java.util.Arrays;
import java.util.List;

import static cd.casic.framework.commons.exception.util.ServiceExceptionUtil.exception;
import static cd.casic.module.machine.contants.MachineErrorCodeConstants.*;

/**
 * 环境变量服务实现类
 */
@Service("machineEnvService")
public class MachineEnvServiceImpl implements MachineEnvService {

    @Resource
    private MachineEnvMapper machineEnvMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createEnv(MachineEnvVO machineEnvVO) {
        validateMachineEnvAdd(machineEnvVO);
        // 检查键是否合法
        validateKey(machineEnvVO.getEnvKey());
        MachineEnvDO machineEnvDO = BeanUtils.toBean(machineEnvVO, MachineEnvDO.class);
        machineEnvMapper.insert(machineEnvDO);
        return machineEnvDO.getId();
    }

    @Override
    public void deleteEnv(Long machineEvnId) {
        machineEnvMapper.deleteById(machineEvnId);
    }


    @Override
    public MachineEnvVO getEnv(Long machineId) {
        MachineEnvDO machineEnvDO = validateMachineEnvExists(machineId);
        return BeanUtils.toBean(machineEnvDO, MachineEnvVO.class);
    }


    @Override
    public PageResult<MachineEnvDO> getEnvPage(MachineEnvVO machineEnvVO) {
        return machineEnvMapper.selectPage(machineEnvVO);
    }

    @Override
    public void deleteEnvList(String ids) {
        //ids转换为List,使用流
        List<Long> machineEnvIds = Arrays.stream(ids.split(","))
            .map(String::trim)
            .filter(s -> !s.isEmpty())
            .map(Long::parseLong)
            .toList();

        machineEnvMapper.deleteBatchIds(machineEnvIds);
    }

    @Override
    public void updateEnv(MachineEnvVO machineEnvVO) {
        MachineEnvDO machineEnvDO = validateMachineEnvExists(machineEnvVO.getId());
        BeanUtils.copyProperties(machineEnvVO, machineEnvDO);
        machineEnvMapper.updateById(machineEnvDO);
    }

    @VisibleForTesting
    MachineEnvDO validateMachineEnvExists(Long id) {
        if (id == null) {
            return null;
        }
        MachineEnvDO machineEnvDO = machineEnvMapper.selectById(id);
        if (machineEnvDO == null) {
            throw exception(MACHINE_ENV_NOT_EXISTS);
        }
        return machineEnvDO;
    }

    @VisibleForTesting
    void validateMachineEnvAdd(MachineEnvVO machineEnvVO) {
        if (machineEnvVO.getEnvKey() == null || machineEnvVO.getEnvValue() == null) {
            throw exception(MACHINE_ENV_NULL);
        }
    }


    // 检查环境变量键是否合法
    @VisibleForTesting
    private void validateKey(String key) {
        if (!key.matches("^[a-zA-Z_][a-zA-Z0-9_]*$")) {
            throw exception(MACHINE_ENV_KEY_ILLEGAL);
        }
    }

}
