package cn.reghao.bnt.web.devops.util;

import cn.reghao.bnt.web.admin.db.repository.SiteOptionRepository;
import cn.reghao.bnt.web.admin.model.po.SiteOption;
import cn.reghao.bnt.web.admin.service.UserContext;
import cn.reghao.bnt.web.devops.app.model.constant.AppType;
import cn.reghao.bnt.web.devops.builder.model.constant.EnvType;
import org.springframework.stereotype.Service;

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

/**
 * env 和 appType 的默认值
 *
 * @author reghao
 * @date 2023-12-01 11:29:21
 */
@Service
public class DefaultSetting {
    private final SiteOptionRepository siteOptionRepository;

    public DefaultSetting(SiteOptionRepository siteOptionRepository) {
        this.siteOptionRepository = siteOptionRepository;
    }

    public List<String> getEnvList() {
        return Arrays.stream(EnvType.values()).map(EnvType::name).collect(Collectors.toList());
    }

    public String getDefaultEnv() {
        SiteOption siteOption = siteOptionRepository.findByPropKey("site.devops.env");
        /*Set<String> roles = UserContext.getUserRoles();
        if (roles.contains(RoleType.ROLE_DEVOPS.name())) {
            return EnvType.test.name();
        } else if (roles.contains(RoleType.ROLE_BACKEND.name())
                || roles.contains(RoleType.ROLE_FRONTEND.name())) {
            return EnvType.test.name();
        }*/
        //return EnvType.test.name();
        return siteOption.getPropValue();
    }

    public List<String> getAppTypeList() {
        return Arrays.stream(AppType.values()).map(AppType::name).collect(Collectors.toList());
    }

    public String getDefaultAppType() {
        String role = UserContext.getUserRole();
        String roleName = role.replace("ROLE_", "").toLowerCase();
        String propKey = String.format("site.devops.appType.%s", roleName);
        SiteOption siteOption = siteOptionRepository.findByPropKey(propKey);
        String defaultAppType = siteOption.getPropValue();
        return defaultAppType;
    }
}
