package com.dora.dto;

import com.dora.entity.WechatMenu;
import com.dora.entity.WechatMessageTemplate;
import com.dora.entity.WechatKeywordReply;
import com.dora.entity.WechatNewsArticle;
import lombok.Data;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;
import io.swagger.v3.oas.annotations.media.Schema;

/**
 * 微信公众号管理相关DTO
 * 
 * @author dora
 * @date 2024/12/01
 */
public class WechatManagementDto {

    // ==================== 菜单管理相关 ====================

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class MenuCreateRequest {
        private Long parentId;
        private String menuName;
        private String menuType;
        private String menuKey;
        private String menuUrl;
        private String mediaId;
        private String appid;
        private String pagepath;
        private Integer sortOrder;
        private String description;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class MenuUpdateRequest {
        private String menuName;
        private String menuType;
        private String menuKey;
        private String menuUrl;
        private String mediaId;
        private String appid;
        private String pagepath;
        private Integer sortOrder;
        private Integer isEnabled;
        private String description;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class MenuPublishResult {
        private boolean success;
        private String message;
        private String errorCode;
        private LocalDateTime publishTime;
        private String menuConfig;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class WechatMenuResponse {
        private boolean exists;
        private String menuConfig;
        private LocalDateTime lastUpdateTime;
    }

    // ==================== 消息模板管理相关 ====================

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class TemplateQueryRequest {
        private String templateName;
        private String templateType;
        private String triggerType;
        private Integer isEnabled;
        private int page = 1;
        private int size = 10;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class TemplateCreateRequest {
        private String templateKey;
        private String templateName;
        private String templateType;
        private String triggerType;
        private String triggerValue;
        private String content;
        private String mediaId;
        private String title;
        private String description;
        private String picUrl;
        private String url;
        private String musicUrl;
        private String hqMusicUrl;
        private String thumbMediaId;
        private Integer priority;
        private List<NewsArticleRequest> articles;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class TemplateUpdateRequest {
        private String templateName;
        private String templateType;
        private String triggerType;
        private String triggerValue;
        private String content;
        private String mediaId;
        private String title;
        private String description;
        private String picUrl;
        private String url;
        private String musicUrl;
        private String hqMusicUrl;
        private String thumbMediaId;
        private Integer isEnabled;
        private Integer priority;
        private List<NewsArticleRequest> articles;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class NewsArticleRequest {
        private Long id;
        private String title;
        private String author;
        private String digest;
        private String content;
        private String contentSourceUrl;
        private String thumbMediaId;
        private Integer showCoverPic;
        private String url;
        private String thumbUrl;
        private Integer needOpenComment;
        private Integer onlyFansCanComment;
        private Integer sortOrder;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class TemplatePageResponse {
        private List<WechatMessageTemplate> templates;
        private long total;
        private int page;
        private int size;
        private int totalPages;
    }

    // ==================== 关键词回复管理相关 ====================

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class KeywordReplyQueryRequest {
        private String keyword;
        private String matchType;
        private Integer isEnabled;
        private int page = 1;
        private int size = 10;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class KeywordReplyCreateRequest {
        private String keyword;
        private String matchType;
        private Long templateId;
        private Integer priority;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class KeywordReplyUpdateRequest {
        private String keyword;
        private String matchType;
        private Long templateId;
        private Integer isEnabled;
        private Integer priority;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class KeywordReplyPageResponse {
        private List<WechatKeywordReply> keywordReplies;
        private long total;
        private int page;
        private int size;
        private int totalPages;
    }

    // ==================== 统计分析相关 ====================

    /**
     * 管理概览统计 DTO
     */
    @Data
    public static class ManagementOverview {
        private long menuCount;
        private long templateCount;
        private long keywordReplyCount;
        private long totalConfigCount;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class MenuStatistics {
        private String menuType;
        private int count;
        private String description;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class TemplateStatistics {
        private String templateType;
        private int count;
        private String description;
    }

    // ==================== 素材管理 DTOs ====================

    /**
     * 永久素材上传结果 DTO
     */
    @Data
    public static class MediaUploadResult {
        @Schema(description = "新增的永久素材的media_id")
        private String mediaId;

        @Schema(description = "新增的永久素材的URL（仅图文消息有）")
        private String url;
    }
    
    /**
     * 永久素材列表项 DTO
     */
    @Data
    public static class MediaFileItem {
        @Schema(description = "素材的media_id")
        private String mediaId;

        @Schema(description = "文件名")
        private String name;

        @Schema(description = "素材的最后更新时间")
        private Long updateTime;

        @Schema(description = "图片素材的URL")
        private String url;
    }

    /**
     * 永久素材列表分页响应 DTO
     */
    @Data
    public static class MediaFilePageResponse {
        @Schema(description = "素材总数")
        private int total;
        
        @Schema(description = "当前页的素材列表")
        private List<MediaFileItem> items;
    }

    // ==================== 微信API响应相关 ====================

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class WechatApiResponse {
        private int errcode;
        private String errmsg;
        private Object data;
        
        public boolean isSuccess() {
            return errcode == 0;
        }
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class MenuPreviewData {
        private List<WechatMenu> menuTree;
        private String menuJson;
        private boolean valid;
        private List<String> validationErrors;
    }
}
