package com.sino.mci.policy.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sino.mci.exception.BusinessException;
import com.sino.mci.policy.dto.CreatePolicyGroupRequest;
import com.sino.mci.policy.dto.UpdatePolicyGroupRequest;
import com.sino.mci.policy.entity.PolicyGroup;
import com.sino.mci.policy.repository.PolicyGroupMapper;
import com.sino.mci.shared.util.JsonUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class PolicyGroupService {

    private final PolicyGroupMapper policyGroupMapper;

    @Transactional
    public PolicyGroup createPolicyGroup(String tenantCode, CreatePolicyGroupRequest request) {
        PolicyGroup existing = policyGroupMapper.selectOne(
                new LambdaQueryWrapper<PolicyGroup>()
                        .eq(PolicyGroup::getTenantCode, tenantCode)
                        .eq(PolicyGroup::getGroupCode, request.getGroupCode()));
        if (existing != null) {
            throw new BusinessException("策略组编码已存在: " + request.getGroupCode());
        }

        PolicyGroup group = new PolicyGroup();
        group.setTenantCode(tenantCode);
        group.setGroupCode(request.getGroupCode());
        group.setGroupName(request.getGroupName());
        group.setChannelType(request.getChannelType());
        group.setPolicies(JsonUtils.toJson(request.getPolicies()));
        group.setStatus(1);
        policyGroupMapper.insert(group);
        return group;
    }

    public PolicyGroup getPolicyGroup(String tenantCode, String groupCode) {
        PolicyGroup group = policyGroupMapper.selectOne(
                new LambdaQueryWrapper<PolicyGroup>()
                        .eq(PolicyGroup::getTenantCode, tenantCode)
                        .eq(PolicyGroup::getGroupCode, groupCode));
        if (group == null) {
            throw new BusinessException("策略组不存在: " + groupCode);
        }
        return group;
    }

    public List<PolicyGroup> listPolicyGroups(String tenantCode) {
        return policyGroupMapper.selectList(
                new LambdaQueryWrapper<PolicyGroup>()
                        .eq(PolicyGroup::getTenantCode, tenantCode)
                        .orderByDesc(PolicyGroup::getCreateTime));
    }

    @Transactional
    public PolicyGroup updatePolicyGroup(String tenantCode, String groupCode, UpdatePolicyGroupRequest request) {
        PolicyGroup group = getPolicyGroup(tenantCode, groupCode);
        group.setGroupName(request.getGroupName());
        group.setChannelType(request.getChannelType());
        group.setPolicies(JsonUtils.toJson(request.getPolicies()));
        policyGroupMapper.updateById(group);
        return group;
    }

    @Transactional
    public PolicyGroup updatePolicyGroupStatus(String tenantCode, String groupCode, Integer status) {
        PolicyGroup group = getPolicyGroup(tenantCode, groupCode);
        group.setStatus(status);
        policyGroupMapper.updateById(group);
        return group;
    }

    @Transactional
    public void deletePolicyGroup(String tenantCode, String groupCode) {
        PolicyGroup group = getPolicyGroup(tenantCode, groupCode);
        policyGroupMapper.deleteById(group.getId());
    }
}
