package com.jeesharp.business.modules.finance.invoice.application;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.finance.invoice.convert.MemberInvoiceTitleConvert;
import com.jeesharp.business.modules.finance.invoice.dto.MemberInvoiceTitleSaveDTO;
import com.jeesharp.business.modules.finance.invoice.entity.MemberInvoiceTitle;
import com.jeesharp.business.modules.finance.invoice.enums.InvoiceTitleDefaultEnum;
import com.jeesharp.business.modules.finance.invoice.service.MemberInvoiceTitleService;
import com.jeesharp.business.modules.finance.invoice.vo.MemberInvoiceTitleInfoVO;
import com.jeesharp.framework.exception.BizErrorException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 发票抬头应用服务：编排会员常用抬头的增删改查与默认设置。
 *
 * @author jeesharp
 * @since 2026-06-12
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MemberInvoiceTitleApplicationService {

    private final MemberInvoiceTitleService memberInvoiceTitleService;

    public List<MemberInvoiceTitleInfoVO> listByMember(String memberId) {
        return memberInvoiceTitleService.listByMember(memberId).stream()
                .map(MemberInvoiceTitleConvert.INSTANCE::toInfoVO)
                .toList();
    }

    public MemberInvoiceTitleInfoVO get(String id) {
        MemberInvoiceTitle entity = memberInvoiceTitleService.get(id);
        return entity == null ? null : MemberInvoiceTitleConvert.INSTANCE.toInfoVO(entity);
    }

    @Transactional(readOnly = false)
    public String create(String memberId, MemberInvoiceTitleSaveDTO dto) {
        if (StrUtil.isNotBlank(dto.getId())) {
            throw new BizErrorException("新增请求不应携带主键 id");
        }
        MemberInvoiceTitle entity = MemberInvoiceTitleConvert.INSTANCE.toEntity(dto);
        entity.setMemberId(memberId);
        if (memberInvoiceTitleService.listByMember(memberId).isEmpty()) {
            entity.setIsDefault(InvoiceTitleDefaultEnum.YES.getValue());
        }
        memberInvoiceTitleService.createTitle(entity);
        return entity.getId();
    }

    @Transactional(readOnly = false)
    public void update(String memberId, MemberInvoiceTitleSaveDTO dto) {
        if (StrUtil.isBlank(dto.getId())) {
            throw new BizErrorException("更新请求必须携带主键 id");
        }
        assertOwned(memberId, dto.getId());
        MemberInvoiceTitle entity = MemberInvoiceTitleConvert.INSTANCE.toEntity(dto);
        entity.setMemberId(memberId);
        memberInvoiceTitleService.updateTitle(entity);
    }

    @Transactional(readOnly = false)
    public void delete(String memberId, String id) {
        assertOwned(memberId, id);
        memberInvoiceTitleService.delete(id);
    }

    @Transactional(readOnly = false)
    public void setDefault(String memberId, String id) {
        memberInvoiceTitleService.setDefaultTitle(id, memberId);
    }

    private void assertOwned(String memberId, String id) {
        MemberInvoiceTitle existing = memberInvoiceTitleService.get(id);
        if (existing == null || !memberId.equals(existing.getMemberId())) {
            throw new BizErrorException("抬头不存在");
        }
    }
}
