package com.dora.entity;

import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 微信消息模板配置实体
 * 
 * @author dora
 * @date 2024/12/01
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class WechatMessageTemplate {

    /**
     * 模板ID
     */
    private Long id;

    /**
     * 模板标识键
     */
    private String templateKey;

    /**
     * 模板名称
     */
    private String templateName;

    /**
     * 模板类型：text/news/image/voice/video/music
     */
    private String templateType;

    /**
     * 触发类型：subscribe/unsubscribe/scan/click/view/keyword
     */
    private String triggerType;

    /**
     * 触发值（如菜单key、关键词等）
     */
    private String triggerValue;

    /**
     * 文本内容
     */
    private String content;

    /**
     * 媒体文件ID
     */
    private String mediaId;

    /**
     * 标题（图文消息用）
     */
    private String title;

    /**
     * 描述（图文消息用）
     */
    private String description;

    /**
     * 图片链接
     */
    private String picUrl;

    /**
     * 跳转链接
     */
    private String url;

    /**
     * 音乐链接
     */
    private String musicUrl;

    /**
     * 高质量音乐链接
     */
    private String hqMusicUrl;

    /**
     * 缩略图媒体ID
     */
    private String thumbMediaId;

    /**
     * 是否启用：0-禁用，1-启用
     */
    private Integer isEnabled;

    /**
     * 优先级，数字越大优先级越高
     */
    private Integer priority;

    /**
     * 创建时间
     */
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    private LocalDateTime updateTime;

    /**
     * 图文消息文章列表（非数据库字段）
     */
    private List<WechatNewsArticle> articles;

    /**
     * 模板类型枚举
     */
    public enum TemplateType {
        TEXT("text", "文本消息"),
        NEWS("news", "图文消息"),
        IMAGE("image", "图片消息"),
        VOICE("voice", "语音消息"),
        VIDEO("video", "视频消息"),
        MUSIC("music", "音乐消息");

        private final String code;
        private final String description;

        TemplateType(String code, String description) {
            this.code = code;
            this.description = description;
        }

        public String getCode() {
            return code;
        }

        public String getDescription() {
            return description;
        }

        public static TemplateType fromCode(String code) {
            for (TemplateType type : values()) {
                if (type.code.equals(code)) {
                    return type;
                }
            }
            return null;
        }
    }

    /**
     * 触发类型枚举
     */
    public enum TriggerType {
        SUBSCRIBE("subscribe", "关注事件"),
        UNSUBSCRIBE("unsubscribe", "取消关注事件"),
        SCAN("scan", "扫码事件"),
        CLICK("click", "菜单点击事件"),
        VIEW("view", "菜单跳转事件"),
        KEYWORD("keyword", "关键词回复"),
        DEFAULT("default", "默认回复");

        private final String code;
        private final String description;

        TriggerType(String code, String description) {
            this.code = code;
            this.description = description;
        }

        public String getCode() {
            return code;
        }

        public String getDescription() {
            return description;
        }

        public static TriggerType fromCode(String code) {
            for (TriggerType type : values()) {
                if (type.code.equals(code)) {
                    return type;
                }
            }
            return null;
        }
    }

    /**
     * 检查模板配置是否有效
     */
    public boolean isValid() {
        if (templateKey == null || templateKey.trim().isEmpty()) {
            return false;
        }
        
        if (templateName == null || templateName.trim().isEmpty()) {
            return false;
        }
        
        if (templateType == null || TemplateType.fromCode(templateType) == null) {
            return false;
        }
        
        if (triggerType == null || TriggerType.fromCode(triggerType) == null) {
            return false;
        }
        
        // 根据模板类型检查必填字段
        TemplateType type = TemplateType.fromCode(templateType);
        switch (type) {
            case TEXT:
                return content != null && !content.trim().isEmpty();
            case IMAGE:
                return mediaId != null && !mediaId.trim().isEmpty();
            case VOICE:
                return mediaId != null && !mediaId.trim().isEmpty();
            case VIDEO:
                return mediaId != null && !mediaId.trim().isEmpty() 
                    && thumbMediaId != null && !thumbMediaId.trim().isEmpty();
            case MUSIC:
                return musicUrl != null && !musicUrl.trim().isEmpty()
                    && thumbMediaId != null && !thumbMediaId.trim().isEmpty();
            case NEWS:
                return articles != null && !articles.isEmpty();
            default:
                return true;
        }
    }

    /**
     * 是否启用
     */
    public boolean isEnabled() {
        return isEnabled != null && isEnabled == 1;
    }

    /**
     * 获取优先级，默认为0
     */
    public int getPriorityValue() {
        return priority != null ? priority : 0;
    }
}
