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.MallBrandConvert;
import com.jeesharp.business.modules.mall.dto.MallBrandSaveDTO;
import com.jeesharp.business.modules.mall.entity.MallBrand;

import com.jeesharp.business.modules.mall.service.MallBrandService;
import com.jeesharp.business.modules.mall.vo.MallBrandInfoVO;
import com.jeesharp.business.modules.mall.vo.MallBrandListVO;
import com.jeesharp.business.modules.mall.vo.MallBrandQueryVO;
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;

/**
 * 商品品牌应用服务，编排品牌 CRUD。
 *
 * @author jeesharp
 * @since 2026-05-07
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MallBrandApplicationService {

    private final MallBrandService mallBrandService;

    public MallBrandInfoVO get(String id) {
        return MallBrandConvert.INSTANCE.toInfoVO(mallBrandService.get(id));
    }

    public PageRespVO<MallBrandListVO> findPage(Page<MallBrand> page, MallBrandQueryVO query) {
        Page<MallBrand> result = mallBrandService.findPage(page, query);
        return MallBrandConvert.INSTANCE.toPageRespVO(result);
    }

    /**
     * 建品用品牌列表：固定返回平台租户下已启用品牌（商户与平台共用）。
     */
    public List<MallBrandListVO> listForPublish() {
        return MallBrandConvert.INSTANCE.toListVO(mallBrandService.listPlatformEnabledForPublish());
    }

    @Transactional(readOnly = false)
    public void save(MallBrandSaveDTO dto) {
        MallBrand entity = MallBrandConvert.INSTANCE.toEntity(dto);
        if (StrUtil.isBlank(dto.getId()) && entity.getStatus() == null) {
            entity.setStatus(DisabledEnabledEnum.ENABLED.getValue());
        }
        if (StrUtil.isBlank(dto.getId())) {
            mallBrandService.createBrand(entity);
            return;
        }
        mallBrandService.updateBrand(entity);
    }

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