package shop.alien.util.type;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import shop.alien.entity.store.LifeNotice;

/**
 * LifeNotice 发送人/接收人 userType、refId 回填工具。
 * <p>
 * senderId 为 system 时仅补接收人；为 user_/store_/lawyer_ 前缀时补发送人与接收人。
 * </p>
 */
@Component
@RequiredArgsConstructor
public class LifeNoticeUtil {

    private static final String SYSTEM_SENDER_ID = "system";

    private final TypeUtil typeUtil;

    public void fillUserTypeAndRefId(LifeNotice notice) {
        if (notice == null) {
            return;
        }
        fillSender(notice);
        fillReceiver(notice);
    }

    private void fillSender(LifeNotice notice) {
        String senderId = notice.getSenderId();
        if (!StringUtils.hasText(senderId) || SYSTEM_SENDER_ID.equals(senderId)) {
            return;
        }
        if (notice.getSenderUserType() != null && notice.getSenderRefId() != null) {
            return;
        }
        if (typeUtil.containsUnderscore(senderId)) {
            applyTypeId(notice, typeUtil.resolveTypeAndId(senderId), true);
        }
    }

    private void fillReceiver(LifeNotice notice) {
        String receiverId = notice.getReceiverId();
        if (!StringUtils.hasText(receiverId)) {
            return;
        }
        if (notice.getReceiverUserType() != null && notice.getReceiverRefId() != null) {
            return;
        }
        if (typeUtil.containsUnderscore(receiverId)) {
            applyTypeId(notice, typeUtil.resolveTypeAndId(receiverId), false);
        }
    }

    private void applyTypeId(LifeNotice notice, PhoneTypeIdResult result, boolean sender) {
        if (result == null) {
            return;
        }
        if (sender) {
            notice.setSenderUserType(result.getType());
            notice.setSenderRefId(result.getId());
        } else {
            notice.setReceiverUserType(result.getType());
            notice.setReceiverRefId(result.getId());
        }
    }

    /**
     * 按接收人 receiver_user_type + receiver_ref_id 查询。
     */
    public void applyReceiverQuery(LambdaQueryWrapper<LifeNotice> wrapper, String receiverId) {
        applyReceiverIdentity(wrapper, receiverId);
    }

    /**
     * 按接收人 receiver_user_type + receiver_ref_id 更新条件。
     */
    public void applyReceiverUpdate(LambdaUpdateWrapper<LifeNotice> wrapper, String receiverId) {
        applyReceiverIdentity(wrapper, receiverId);
    }

    /**
     * 按发送人 sender_user_type + sender_ref_id 查询。
     */
    public void applySenderQuery(LambdaQueryWrapper<LifeNotice> wrapper, String senderId) {
        if (!StringUtils.hasText(senderId) || SYSTEM_SENDER_ID.equals(senderId)) {
            wrapper.apply("1 = 0");
            return;
        }
        PhoneTypeIdResult resolved = typeUtil.resolveTypeAndId(senderId);
        if (resolved != null && resolved.getType() != null && resolved.getId() != null) {
            wrapper.eq(LifeNotice::getSenderUserType, resolved.getType())
                    .eq(LifeNotice::getSenderRefId, resolved.getId());
        } else {
            wrapper.apply("1 = 0");
        }
    }

    private void applyReceiverIdentity(LambdaQueryWrapper<LifeNotice> wrapper, String receiverId) {
        if (!StringUtils.hasText(receiverId)) {
            wrapper.apply("1 = 0");
            return;
        }
        PhoneTypeIdResult resolved = typeUtil.resolveTypeAndId(receiverId);
        if (resolved != null && resolved.getType() != null && resolved.getId() != null) {
            wrapper.eq(LifeNotice::getReceiverUserType, resolved.getType())
                    .eq(LifeNotice::getReceiverRefId, resolved.getId());
        } else {
            wrapper.apply("1 = 0");
        }
    }

    private void applyReceiverIdentity(LambdaUpdateWrapper<LifeNotice> wrapper, String receiverId) {
        if (!StringUtils.hasText(receiverId)) {
            wrapper.apply("1 = 0");
            return;
        }
        PhoneTypeIdResult resolved = typeUtil.resolveTypeAndId(receiverId);
        if (resolved != null && resolved.getType() != null && resolved.getId() != null) {
            wrapper.eq(LifeNotice::getReceiverUserType, resolved.getType())
                    .eq(LifeNotice::getReceiverRefId, resolved.getId());
        } else {
            wrapper.apply("1 = 0");
        }
    }
}
