package com.sino.mci.crm.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sino.mci.crm.dto.CreateTagRequest;
import com.sino.mci.crm.dto.UpdateTagRequest;
import com.sino.mci.crm.entity.Tag;
import com.sino.mci.crm.repository.TagMapper;
import com.sino.mci.exception.BusinessException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class TagService {

    private final TagMapper tagMapper;

    @Transactional
    public Tag createTag(String tenantCode, CreateTagRequest request) {
        Long parentId = request.getParentId() != null ? request.getParentId() : 0L;
        int level = 1;
        String parentPath = "";

        if (parentId > 0) {
            Tag parent = tagMapper.selectById(parentId);
            if (parent == null || !tenantCode.equals(parent.getTenantCode())) {
                throw new BusinessException("父标签不存在");
            }
            level = parent.getLevel() + 1;
            parentPath = parent.getTagPath();
        }

        String tagPath = parentPath.isEmpty() ? request.getTagName() : parentPath + "/" + request.getTagName();

        Tag existing = tagMapper.selectOne(
                new LambdaQueryWrapper<Tag>()
                        .eq(Tag::getTenantCode, tenantCode)
                        .eq(Tag::getTagPath, tagPath));
        if (existing != null) {
            throw new BusinessException("标签路径已存在: " + tagPath);
        }

        Tag tag = new Tag();
        tag.setTenantCode(tenantCode);
        tag.setParentId(parentId);
        tag.setTagPath(tagPath);
        tag.setLevel(level);
        tag.setTagName(request.getTagName());
        tag.setTagSource(request.getTagSource() != null ? request.getTagSource() : "manual");
        tag.setStatus(1);
        tagMapper.insert(tag);
        return tag;
    }

    public Tag getTag(String tenantCode, Long tagId) {
        Tag tag = tagMapper.selectById(tagId);
        if (tag == null || !tenantCode.equals(tag.getTenantCode())) {
            throw new BusinessException("标签不存在");
        }
        return tag;
    }

    @Transactional
    public Tag updateTag(String tenantCode, Long tagId, UpdateTagRequest request) {
        Tag tag = getTag(tenantCode, tagId);
        if (request.getStatus() != null) {
            tag.setStatus(request.getStatus());
        }
        tagMapper.updateById(tag);
        return tag;
    }

    @Transactional
    public void deleteTag(String tenantCode, Long tagId) {
        getTag(tenantCode, tagId);
        tagMapper.deleteById(tagId);
    }

    public List<Tag> listTags(String tenantCode) {
        return tagMapper.selectList(
                new LambdaQueryWrapper<Tag>()
                        .eq(Tag::getTenantCode, tenantCode)
                        .eq(Tag::getStatus, 1)
                        .orderByAsc(Tag::getTagPath));
    }
}
