package com.jeesharp.business.modules.setting.convert;

import cn.hutool.json.JSONUtil;
import com.jeesharp.business.modules.setting.dto.FeedbackSubmitDTO;
import com.jeesharp.business.modules.setting.entity.Feedback;
import com.jeesharp.business.modules.setting.vo.FeedbackVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * 意见反馈转换器
 *
 * @author jeesharp
 * @version 2026-02-05
 */
@Mapper
public interface FeedbackConvert {

    FeedbackConvert INSTANCE = Mappers.getMapper(FeedbackConvert.class);

    /**
     * Entity 转 VO
     */
    @Mapping(target = "feedbackTypeName", source = "feedbackType", qualifiedByName = "feedbackTypeToName")
    @Mapping(target = "statusName", source = "status", qualifiedByName = "statusToName")
    @Mapping(target = "images", source = "images", qualifiedByName = "jsonStringToList")
    FeedbackVO toVO(Feedback entity);

    /**
     * Entity 列表转 VO 列表
     */
    List<FeedbackVO> toVOList(List<Feedback> list);

    /**
     * SubmitDTO 转 Entity
     */
    @Mapping(target = "images", source = "images", qualifiedByName = "listToJsonString")
    @Mapping(target = "status", constant = "0")
    @Mapping(target = "id", ignore = true)
    @Mapping(target = "memberId", ignore = true)
    @Mapping(target = "handlerId", ignore = true)
    @Mapping(target = "handlerName", ignore = true)
    @Mapping(target = "handleDate", ignore = true)
    @Mapping(target = "handleResult", ignore = true)
    @Mapping(target = "createById", ignore = true)
    @Mapping(target = "createDate", ignore = true)
    @Mapping(target = "updateById", ignore = true)
    @Mapping(target = "updateDate", ignore = true)
    @Mapping(target = "delFlag", ignore = true)
    @Mapping(target = "remarks", ignore = true)
    @Mapping(target = "tenantCode", ignore = true)
    @Mapping(target = "tenantId", ignore = true)
    Feedback toEntity(FeedbackSubmitDTO dto);

    // ==================== 转换方法 ====================

    /**
     * 反馈类型转名称
     */
    @Named("feedbackTypeToName")
    default String feedbackTypeToName(Integer feedbackType) {
        if (feedbackType == null) {
            return "";
        }
        return switch (feedbackType) {
            case 1 -> "功能建议";
            case 2 -> "问题反馈";
            case 3 -> "其他";
            default -> "未知";
        };
    }

    /**
     * 状态转名称
     */
    @Named("statusToName")
    default String statusToName(Integer status) {
        if (status == null) {
            return "";
        }
        return switch (status) {
            case 0 -> "待处理";
            case 1 -> "处理中";
            case 2 -> "已处理";
            case 3 -> "已关闭";
            default -> "未知";
        };
    }

    /**
     * JSON字符串转List
     */
    @Named("jsonStringToList")
    default List<String> jsonStringToList(String jsonStr) {
        if (jsonStr == null || jsonStr.trim().isEmpty()) {
            return List.of();
        }
        try {
            return JSONUtil.toList(jsonStr, String.class);
        } catch (Exception e) {
            return List.of();
        }
    }

    /**
     * List转JSON字符串
     */
    @Named("listToJsonString")
    default String listToJsonString(List<String> list) {
        if (list == null || list.isEmpty()) {
            return null;
        }
        try {
            return JSONUtil.toJsonStr(list);
        } catch (Exception e) {
            return null;
        }
    }
}
