package com.dora.dto;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Data;

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

/**
 * 管理员内容审核 DTO
 */
public class AdminContentAuditDto {

    @Data
    @Schema(description = "内容审核列表请求")
    public static class ContentAuditListRequest {
        @Schema(description = "页码", example = "1")
        private Integer page = 1;

        @Schema(description = "每页数量", example = "10")
        private Integer size = 10;

        @Schema(description = "内容类型筛选（video/workflow/course）", example = "video")
        private String contentType;

        @Schema(description = "审核状态筛选（0待审核/1通过/2拒绝）", example = "0")
        private Integer auditStatus;

        @Schema(description = "创建者ID筛选", example = "123")
        private Long ownerId;

        @Schema(description = "关键词搜索（标题/描述）", example = "测试")
        private String keyword;

        @Schema(description = "创建时间开始", example = "2024-01-01T00:00:00")
        private LocalDateTime createTimeStart;

        @Schema(description = "创建时间结束", example = "2024-01-31T23:59:59")
        private LocalDateTime createTimeEnd;

        @Schema(description = "排序字段（create_time/review_time）", example = "create_time")
        private String sortField = "create_time";

        @Schema(description = "排序方向（asc/desc）", example = "desc")
        private String sortOrder = "desc";
    }

    @Data
    @Schema(description = "内容审核列表响应")
    public static class ContentAuditListResponse {
        @Schema(description = "总数")
        private Long total;

        @Schema(description = "页码")
        private Long page;

        @Schema(description = "每页数量")
        private Long size;

        @Schema(description = "内容列表")
        private List<ContentAuditItem> records;
    }

    @Data
    @Schema(description = "内容审核项")
    public static class ContentAuditItem {
        @JsonSerialize(using = ToStringSerializer.class)
        @Schema(description = "内容ID（数据库ID，用于审核操作）")
        private Long id;

        @Schema(description = "内容类型（video/workflow/course）")
        private String contentType;

        @Schema(description = "内容标题")
        private String title;

        @Schema(description = "内容描述")
        private String description;

        @JsonSerialize(using = ToStringSerializer.class)
        @Schema(description = "创建者ID")
        private Long ownerId;

        @Schema(description = "创建者用户名")
        private String ownerUsername;

        @Schema(description = "创建者角色（0游客/1普通/2VIP/3SVIP）")
        private Integer ownerRole;

        @Schema(description = "审核状态（0待审核/1通过/2拒绝）")
        private Integer auditStatus;

        @Schema(description = "审核状态名称")
        private String auditStatusName;

        @Schema(description = "拒绝原因")
        private String rejectReason;

        @Schema(description = "审核时间")
        private LocalDateTime reviewTime;

        @Schema(description = "创建时间")
        private LocalDateTime createTime;

        @Schema(description = "更新时间")
        private LocalDateTime updateTime;

        @Schema(description = "封面图片URL")
        private String coverUrl;

        @Schema(description = "分类")
        private String category;

        @Schema(description = "是否公开（0私有/1公开）")
        private Integer isPublic;

        // 工作流特有字段
        @Schema(description = "是否免费（工作流/课程）")
        private Integer isFree;

        @Schema(description = "查看权限角色（工作流/课程）")
        private Integer fullAccessRole;

        @Schema(description = "复制权限角色（工作流）")
        private Integer copyAccessRole;

        // 视频特有字段
        @Schema(description = "视频时长（视频）")
        private Integer duration;

        @Schema(description = "观看次数")
        private Integer viewCount;

        @Schema(description = "点赞数量")
        private Integer likeCount;

        // 课程特有字段
        @Schema(description = "课程访问级别（课程）")
        private Integer courseLevel;

        @Schema(description = "课程包含的阿里云视频ID列表（逗号分隔）")
        private String videoIds;

        @Schema(description = "课程包含的视频标题列表（逗号分隔）")
        private String videoTitles;

        @Schema(description = "课程章节数量")
        private Integer chapterCount;

        @Schema(description = "课程视频数量")
        private Integer videoCount;

        @Schema(description = "阿里云VOD视频ID（仅视频类型有效）")
        private String vodVideoId;

        // 工作流特有字段
        @Schema(description = "工作流核心逻辑JSON数据")
        private String workflowData;

        @Schema(description = "工作流依赖文件下载URL")
        private String dataFileUrl;

        @Schema(description = "详情图集（JSON格式存储多张图片URL）")
        private String detailGallery;
    }

    @Data
    @Schema(description = "内容审核操作请求")
    public static class ContentAuditRequest {
        @NotNull(message = "内容ID不能为空")
        @Schema(description = "内容ID", example = "123")
        private Long contentId;

        @NotNull(message = "内容类型不能为空")
        @Schema(description = "内容类型（video/workflow/course）", example = "video")
        private String contentType;

        @NotNull(message = "审核状态不能为空")
        @Schema(description = "审核状态（1通过/2拒绝）", example = "1", allowableValues = {"1", "2"})
        private Integer auditStatus;

        @Schema(description = "拒绝原因（审核拒绝时必填）", example = "内容不符合规范")
        private String rejectReason;
    }

    @Data
    @Schema(description = "批量审核请求")
    public static class BatchAuditRequest {
        @NotNull(message = "审核项目不能为空")
        @Schema(description = "审核项目列表")
        private List<ContentAuditRequest> auditItems;
    }

    @Data
    @Schema(description = "审核统计信息")
    public static class AuditStatistics {
        @Schema(description = "待审核总数")
        private Long pendingTotal;

        @Schema(description = "待审核视频数")
        private Long pendingVideos;

        @Schema(description = "待审核工作流数")
        private Long pendingWorkflows;

        @Schema(description = "待审核课程数")
        private Long pendingCourses;

        @Schema(description = "今日审核通过总数")
        private Long todayApprovedTotal;

        @Schema(description = "今日审核拒绝总数")
        private Long todayRejectedTotal;
    }

    @Data
    @Schema(description = "课程属性更新请求")
    public static class CoursePropertiesUpdateRequest {
        @NotNull(message = "课程ID不能为空")
        @Schema(description = "课程ID", example = "123")
        private Long courseId;

        @Schema(description = "是否免费（0收费/1免费）", example = "0")
        private Integer isFree;

        @Schema(description = "访问级别（0游客/1普通/2VIP/3SVIP）", example = "2")
        private Integer level;

        @Schema(description = "课程分类", example = "教育")
        private String category;
    }

    @Data
    @Schema(description = "工作流属性更新请求")
    public static class WorkflowPropertiesUpdateRequest {
        @NotNull(message = "工作流ID不能为空")
        @Schema(description = "工作流ID", example = "123")
        private Long workflowId;

        @Schema(description = "是否免费（0收费/1免费）", example = "0")
        private Integer isFree;

        @Schema(description = "查看权限角色（0游客/1普通/2VIP/3SVIP）", example = "2")
        private Integer fullAccessRole;

        @Schema(description = "复制权限角色（0游客/1普通/2VIP/3SVIP）", example = "3")
        private Integer copyAccessRole;

        @Schema(description = "工作流分类", example = "自动化")
        private String category;

        @Schema(description = "评分（1-5）", example = "4")
        private Integer rating;
    }

    @Data
    @Schema(description = "管理员视频播放凭证请求")
    public static class VideoPlayAuthRequest {
        @NotNull(message = "阿里云视频ID不能为空")
        @Schema(description = "阿里云VOD视频ID", example = "30e9e19b744371f0bf9887c6371c0102")
        private String vodVideoId;

        @Schema(description = "课程ID（可选，用于权限验证）", example = "123")
        private Long courseId;

        @Schema(description = "章节ID（可选，用于权限验证）", example = "456")
        private Long chapterId;
    }

    /**
     * 审核状态枚举
     */
    public enum AuditStatus {
        PENDING(0, "待审核"),
        APPROVED(1, "审核通过"),
        REJECTED(2, "审核拒绝");

        private final Integer code;
        private final String name;

        AuditStatus(Integer code, String name) {
            this.code = code;
            this.name = name;
        }

        public Integer getCode() {
            return code;
        }

        public String getName() {
            return name;
        }

        public static String getNameByCode(Integer code) {
            for (AuditStatus status : AuditStatus.values()) {
                if (status.code.equals(code)) {
                    return status.name;
                }
            }
            return "未知状态";
        }
    }

    /**
     * 内容类型枚举
     */
    public enum ContentType {
        VIDEO("video", "视频"),
        WORKFLOW("workflow", "工作流"),
        COURSE("course", "课程");

        private final String code;
        private final String name;

        ContentType(String code, String name) {
            this.code = code;
            this.name = name;
        }

        public String getCode() {
            return code;
        }

        public String getName() {
            return name;
        }

        public static String getNameByCode(String code) {
            for (ContentType type : ContentType.values()) {
                if (type.code.equals(code)) {
                    return type.name;
                }
            }
            return "未知类型";
        }
    }
}
