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

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.member.dto.MemberVisitorBodyDTO;
import com.jeesharp.business.modules.member.entity.MemberVisitor;
import com.jeesharp.business.modules.member.service.MemberVisitorService;
import com.jeesharp.business.modules.member.vo.MemberVisitorItemVO;
import com.jeesharp.framework.utils.base.ExceptionUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 会员常用游客应用服务：小程序列表、增删改及条数、身份证号唯一校验。
 *
 * @author jeesharp
 * @since 2026-05-12
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MemberVisitorApplicationService {

    /**
     * 单会员常用游客上限（与小程序 {@code MAX_VISITORS} 对齐）。
     */
    private static final int MAX_VISITORS_PER_MEMBER = 20;

    private final MemberVisitorService memberVisitorService;

    public List<MemberVisitorItemVO> listForMobile(String memberId) {
        return memberVisitorService.listByMemberId(memberId).stream()
                .map(this::toItem)
                .toList();
    }

    @Transactional(readOnly = false)
    public MemberVisitorItemVO create(String memberId, MemberVisitorBodyDTO dto) {
        if (memberVisitorService.countByMemberId(memberId) >= MAX_VISITORS_PER_MEMBER) {
            ExceptionUtil.error("常用游客最多保存" + MAX_VISITORS_PER_MEMBER + "位");
        }
        String idCard = dto.getIdCard().trim().toUpperCase();
        if (memberVisitorService.existsIdCardForMember(memberId, idCard, null)) {
            ExceptionUtil.error("该身份证号已添加过");
        }
        MemberVisitor e = new MemberVisitor();
        e.setMemberId(memberId);
        e.setVisitorName(dto.getName().trim());
        e.setIdCard(idCard);
        e.setMobile(StrUtil.isBlank(dto.getPhone()) ? null : dto.getPhone().trim());
        memberVisitorService.insert(e);
        MemberVisitor saved = memberVisitorService.getOwned(e.getId(), memberId);
        return toItem(saved);
    }

    @Transactional(readOnly = false)
    public MemberVisitorItemVO update(String memberId, String id, MemberVisitorBodyDTO dto) {
        MemberVisitor old = memberVisitorService.getOwned(id, memberId);
        if (old == null) {
            ExceptionUtil.error("游客不存在");
        }
        String idCard = dto.getIdCard().trim().toUpperCase();
        if (memberVisitorService.existsIdCardForMember(memberId, idCard, id)) {
            ExceptionUtil.error("该身份证号已添加过");
        }
        old.setVisitorName(dto.getName().trim());
        old.setIdCard(idCard);
        old.setMobile(StrUtil.isBlank(dto.getPhone()) ? null : dto.getPhone().trim());
        memberVisitorService.updateEntity(old);
        return toItem(memberVisitorService.getOwned(id, memberId));
    }

    @Transactional(readOnly = false)
    public void delete(String memberId, String id) {
        if (memberVisitorService.getOwned(id, memberId) == null) {
            ExceptionUtil.error("游客不存在");
        }
        memberVisitorService.removeOwned(id, memberId);
    }

    private MemberVisitorItemVO toItem(MemberVisitor e) {
        if (e == null) {
            return null;
        }
        MemberVisitorItemVO vo = new MemberVisitorItemVO();
        vo.setId(e.getId());
        vo.setName(e.getVisitorName());
        vo.setIdCard(e.getIdCard());
        vo.setPhone(e.getMobile());
        return vo;
    }
}
