package com.jeesharp.business.modules.mall.application;

import com.jeesharp.business.modules.common.enums.DisabledEnabledEnum;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.mall.convert.MallSpecValueConvert;
import com.jeesharp.business.modules.mall.dto.MallSpecValueCreateForPublishDTO;
import com.jeesharp.business.modules.mall.dto.MallSpecValueSaveDTO;
import com.jeesharp.business.modules.mall.entity.MallSpecValue;

import com.jeesharp.business.modules.mall.service.MallSpecValueService;
import com.jeesharp.business.modules.mall.vo.MallSpecValueInfoVO;
import com.jeesharp.business.modules.mall.vo.MallSpecValueListVO;
import com.jeesharp.business.modules.mall.vo.MallSpecValueQueryVO;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 规格值应用服务。
 *
 * @author jeesharp
 * @since 2026-05-07
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MallSpecValueApplicationService {

    private final MallSpecValueService mallSpecValueService;

    public MallSpecValueInfoVO get(String id) {
        return MallSpecValueConvert.INSTANCE.toInfoVO(mallSpecValueService.get(id));
    }

    public PageRespVO<MallSpecValueListVO> findPage(Page<MallSpecValue> page, MallSpecValueQueryVO query) {
        Page<MallSpecValue> result = mallSpecValueService.findPage(page, query);
        return MallSpecValueConvert.INSTANCE.toPageRespVO(result);
    }

    /**
     * 建品可选规格值：当前商户租户、指定平台规格维度下已启用值。
     */
    public List<MallSpecValueListVO> listForPublish(MallSpecValueQueryVO query) {
        String specId = query != null ? query.getSpecId() : null;
        return MallSpecValueConvert.INSTANCE.toListVO(mallSpecValueService.listEnabledForPublish(specId));
    }

    /**
     * 发品页新增规格值（当前租户；同维度同文案去重返回已有行）。
     */
    @Transactional(readOnly = false)
    public MallSpecValueListVO createForPublish(MallSpecValueCreateForPublishDTO dto) {
        MallSpecValue entity = mallSpecValueService.createOrGetForPublish(dto.getSpecId(), dto.getSpecValue());
        List<MallSpecValueListVO> rows = MallSpecValueConvert.INSTANCE.toListVO(List.of(entity));
        return rows.isEmpty() ? null : rows.get(0);
    }

    @Transactional(readOnly = false)
    public void save(MallSpecValueSaveDTO dto) {
        MallSpecValue entity = MallSpecValueConvert.INSTANCE.toEntity(dto);
        if (StrUtil.isBlank(dto.getId()) && entity.getStatus() == null) {
            entity.setStatus(DisabledEnabledEnum.ENABLED.getValue());
        }
        if (StrUtil.isBlank(dto.getId())) {
            mallSpecValueService.createSpecValue(entity);
            return;
        }
        mallSpecValueService.updateSpecValue(entity);
    }

    @Transactional(readOnly = false)
    public void delete(String ids) {
        mallSpecValueService.batchDelete(StrUtil.splitTrim(ids, ','));
    }
}
