package com.dora.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;

/**
 * 管理员认证相关DTO
 */
public class AdminAuthDto {

    @Data
    @Schema(description = "管理员注册请求")
    public static class AdminRegisterRequest {
        @NotBlank(message = "用户名不能为空")
        @Size(min = 3, max = 20, message = "用户名长度必须在3-20个字符之间")
        @Pattern(regexp = "^[a-zA-Z0-9_]+$", message = "用户名只能包含字母、数字和下划线")
        @Schema(description = "管理员用户名", example = "admin001")
        private String username;

        @NotBlank(message = "密码不能为空")
        @Size(min = 6, max = 20, message = "密码长度必须在6-20个字符之间")
        @Schema(description = "密码", example = "123456")
        private String password;

        @NotBlank(message = "确认密码不能为空")
        @Schema(description = "确认密码", example = "123456")
        private String confirmPassword;

        @Schema(description = "角色（0工作人员/1管理员）", example = "1")
        private Integer role = 1; // 默认管理员角色
    }

    @Data
    @Schema(description = "管理员登录请求")
    public static class AdminLoginRequest {
        @NotBlank(message = "用户名不能为空")
        @Schema(description = "管理员用户名", example = "admin001")
        private String username;

        @NotBlank(message = "密码不能为空")
        @Schema(description = "密码", example = "123456")
        private String password;
    }

    @Data
    @Schema(description = "管理员登录响应")
    public static class AdminLoginResponse {
        @Schema(description = "管理员ID")
        private Long adminId;

        @Schema(description = "用户名")
        private String username;

        @Schema(description = "角色")
        private Integer role;

        @Schema(description = "角色名称")
        private String roleName;

        @Schema(description = "访问令牌")
        private String token;

        @Schema(description = "令牌类型")
        private String tokenType = "Bearer";

        @Schema(description = "令牌过期时间")
        private LocalDateTime expiresAt;

        @Schema(description = "权限列表")
        private String[] permissions;
    }

    @Data
    @Schema(description = "管理员刷新Token响应")
    public static class AdminRefreshTokenResponse {
        @Schema(description = "是否成功")
        private boolean success;

        @Schema(description = "是否刷新了token（false表示token无需刷新）")
        private boolean refreshed;

        @Schema(description = "新的或原始的JWT令牌", example = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...")
        private String token;

        @Schema(description = "令牌过期时间")
        private LocalDateTime expiresAt;

        @Schema(description = "响应消息")
        private String message;
    }

    @Data
    @Schema(description = "管理员信息响应")
    public static class AdminInfoResponse {
        @Schema(description = "管理员ID")
        private Long adminId;

        @Schema(description = "用户名")
        private String username;

        @Schema(description = "角色")
        private Integer role;

        @Schema(description = "角色名称")
        private String roleName;

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

        @Schema(description = "权限列表")
        private String[] permissions;
    }

    @Data
    @Schema(description = "仪表盘数据响应")
    public static class DashboardResponse {
        @Schema(description = "用户统计")
        private UserStats userStats;

        @Schema(description = "内容统计")
        private ContentStats contentStats;

        @Schema(description = "财务统计")
        private FinanceStats financeStats;

        @Schema(description = "提现审核统计")
        private WithdrawStats withdrawStats;

        @Schema(description = "今日数据")
        private TodayStats todayStats;
    }

    @Data
    @Schema(description = "用户统计")
    public static class UserStats {
        @Schema(description = "总用户数")
        private Long totalUsers;

        @Schema(description = "今日新增用户")
        private Long todayNewUsers;

        @Schema(description = "活跃用户数")
        private Long activeUsers;

        @Schema(description = "付费用户数")
        private Long paidUsers;
    }

    @Data
    @Schema(description = "内容统计")
    public static class ContentStats {
        @Schema(description = "总工作流数")
        private Long totalWorkflows;

        @Schema(description = "今日新增工作流")
        private Long todayNewWorkflows;

        @Schema(description = "总视频数")
        private Long totalVideos;

        @Schema(description = "今日新增视频")
        private Long todayNewVideos;

        @Schema(description = "待审核内容数")
        private Long pendingAuditContent;
    }

    @Data
    @Schema(description = "财务统计")
    public static class FinanceStats {
        @Schema(description = "总收入")
        private java.math.BigDecimal totalRevenue;

        @Schema(description = "今日收入")
        private java.math.BigDecimal todayRevenue;

        @Schema(description = "总提现金额")
        private java.math.BigDecimal totalWithdrawn;

        @Schema(description = "用户余额总和")
        private java.math.BigDecimal totalUserBalance;
    }

    @Data
    @Schema(description = "提现审核统计")
    public static class WithdrawStats {
        @Schema(description = "待审核提现数量")
        private Long pendingCount;

        @Schema(description = "待审核提现金额")
        private java.math.BigDecimal pendingAmount;

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

        @Schema(description = "今日审核通过金额")
        private java.math.BigDecimal todayApprovedAmount;
    }

    @Data
    @Schema(description = "今日数据统计")
    public static class TodayStats {
        @Schema(description = "今日新增用户")
        private Long newUsers;

        @Schema(description = "今日收入")
        private java.math.BigDecimal revenue;

        @Schema(description = "今日工作流复制次数")
        private Long workflowCopies;

        @Schema(description = "今日视频观看次数")
        private Long videoViews;

        @Schema(description = "今日提现申请数量")
        private Long withdrawRequests;
    }

    /**
     * 权限常量
     */
    public static class Permissions {
        // 管理员权限
        public static final String[] ADMIN_PERMISSIONS = {
            "user:read", "user:write", "user:delete",
            "content:read", "content:write", "content:delete", "content:audit",
            "withdraw:read", "withdraw:review", "withdraw:stats",
            "finance:read", "finance:stats",
            "admin:read", "admin:write", "admin:delete",
            "dashboard:read"
        };

        // 工作人员权限
        public static final String[] STAFF_PERMISSIONS = {
            "user:read",
            "content:read", "content:audit",
            "withdraw:read", "withdraw:review",
            "dashboard:read"
        };

        public static String[] getPermissionsByRole(Integer role) {
            if (role != null && role == 1) {
                return ADMIN_PERMISSIONS;
            }
            return STAFF_PERMISSIONS;
        }
    }
} 