package com.dora.dto;

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

import jakarta.validation.constraints.*;
import java.time.LocalDateTime;
import java.util.List;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;

/**
 * 后台管理端用户管理相关DTO
 */
public class AdminUserDto {

    /**
     * 用户列表查询请求
     */
    @Data
    @Schema(description = "用户列表查询请求")
    public static class UserListRequest {
        @Schema(description = "页码", example = "1")
        @Min(value = 1, message = "页码必须大于0")
        private Integer page = 1;

        @Schema(description = "每页大小", example = "20")
        @Min(value = 1, message = "每页大小必须大于0")
        @Max(value = 100, message = "每页大小不能超过100")
        private Integer size = 20;

        @Schema(description = "搜索关键词（用户名、手机号）")
        private String keyword;

        @Schema(description = "用户角色筛选（0游客/1普通/2VIP/3SVIP）")
        private Integer role;

        @Schema(description = "实名认证状态筛选（0未认证/1已认证）")
        private Integer isVerified;

        @Schema(description = "推广等级筛选")
        private Integer promotionLevel;

        @Schema(description = "注册时间开始")
        private LocalDateTime createTimeStart;

        @Schema(description = "注册时间结束")
        private LocalDateTime createTimeEnd;

        @Schema(description = "排序字段", example = "createTime")
        private String sortField = "createTime";

        @Schema(description = "排序方向", example = "desc")
        private String sortOrder = "desc";

        @Schema(description = "会员类型筛选（all全部/paid当前付费会员/exchange当前兑换会员/gift赠送会员/expired过期会员/paidExpired付费过期会员/exchangeExpired兑换过期会员）")
        private String membershipType;
        
        @Schema(description = "是否禁用筛选（null全部/0正常/1禁用）")
        private Integer isDeleted;
    }

    /**
     * 用户列表项
     */
    @Data
    @Schema(description = "用户列表项")
    public static class UserListItem {
        @Schema(description = "用户ID")
        @JsonSerialize(using = ToStringSerializer.class)
        private Long id;

        @Schema(description = "手机号")
        private String phone;

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

        @Schema(description = "真实用户名")
        private String realUsername;

        @Schema(description = "身份证号码")
        private String idNumber;

        @Schema(description = "用户角色（0游客/1普通/2VIP/3SVIP）")
        private Integer role;

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

        @Schema(description = "会员到期时间")
        private LocalDateTime membershipExpiresAt;

        @Schema(description = "头像URL")
        private String avatarUrl;

        @Schema(description = "是否实名认证（0未认证/1已认证）")
        private Integer isVerified;

        @Schema(description = "邀请码")
        private String inviteCode;

        @Schema(description = "邀请人ID")
        @JsonSerialize(using = ToStringSerializer.class)
        private Long inviterId;

        @Schema(description = "邀请人用户名")
        private String inviterUsername;

        @Schema(description = "推广等级")
        private Integer promotionLevel;

        @Schema(description = "积分余额")
        private Integer points;

        @Schema(description = "积分过期时间")
        private LocalDateTime pointsExpiresAt;

        @Schema(description = "注册时间")
        private LocalDateTime createTime;

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

        @Schema(description = "粉丝数量")
        private Integer fanCount;

        @Schema(description = "付费粉丝数量")
        private Integer paidFanCount;

        @Schema(description = "总提成金额")
        private String totalCommission;
        
        @Schema(description = "是否禁用（0正常/1禁用）")
        private Integer isDeleted;
    }

    /**
     * 用户列表响应
     */
    @Data
    @Schema(description = "用户列表响应")
    public static class UserListResponse {
        @Schema(description = "用户列表")
        private List<UserListItem> users;

        @Schema(description = "总数")
        private Long total;

        @Schema(description = "当前页")
        private Integer page;

        @Schema(description = "每页大小")
        private Integer size;

        @Schema(description = "总页数")
        private Integer totalPages;
    }

    /**
     * 用户详情响应
     */
    @Data
    @Schema(description = "用户详情响应")
    public static class UserDetailResponse {
        @Schema(description = "用户ID")
        @JsonSerialize(using = ToStringSerializer.class)
        private Long id;

        @Schema(description = "手机号")
        private String phone;

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

        @Schema(description = "真实用户名")
        private String realUsername;

        @Schema(description = "身份证号码")
        private String idNumber;

        @Schema(description = "用户角色（0游客/1普通/2VIP/3SVIP）")
        private Integer role;

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

        @Schema(description = "会员到期时间")
        private LocalDateTime membershipExpiresAt;

        @Schema(description = "头像URL")
        private String avatarUrl;

        @Schema(description = "是否实名认证（0未认证/1已认证）")
        private Integer isVerified;

        @Schema(description = "邀请码")
        private String inviteCode;

        @Schema(description = "邀请人ID")
        @JsonSerialize(using = ToStringSerializer.class)
        private Long inviterId;

        @Schema(description = "邀请人用户名")
        private String inviterUsername;

        @Schema(description = "推广等级")
        private Integer promotionLevel;

        @Schema(description = "积分余额")
        private Integer points;

        @Schema(description = "积分过期时间")
        private LocalDateTime pointsExpiresAt;

        @Schema(description = "注册时间")
        private LocalDateTime createTime;

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

        @Schema(description = "粉丝数量")
        private Integer fanCount;

        @Schema(description = "付费粉丝数量")
        private Integer paidFanCount;

        @Schema(description = "总提成金额")
        private String totalCommission;

        @Schema(description = "余额信息")
        private BalanceInfo balanceInfo;
    }

    /**
     * 用户更新请求
     */
    @Data
    @Schema(description = "用户更新请求")
    public static class UserUpdateRequest {
        @Schema(description = "用户ID")
        @NotNull(message = "用户ID不能为空")
        private Long userId;

        @Schema(description = "用户名")
        @Size(max = 50, message = "用户名长度不能超过50个字符")
        private String username;

        @Schema(description = "真实用户名")
        @Size(max = 50, message = "真实用户名长度不能超过50个字符")
        private String realUsername;

        @Schema(description = "身份证号码")
        private String idNumber;

        @Schema(description = "用户角色（0游客/1普通/2VIP/3SVIP）")
        @Min(value = 0, message = "角色值不正确")
        @Max(value = 3, message = "角色值不正确")
        private Integer role;

        @Schema(description = "会员到期时间")
        private LocalDateTime membershipExpiresAt;

        @Schema(description = "是否实名认证（0未认证/1已认证）")
        @Min(value = 0, message = "认证状态值不正确")
        @Max(value = 1, message = "认证状态值不正确")
        private Integer isVerified;

        @Schema(description = "推广等级")
        @Min(value = 0, message = "推广等级不能为负数")
        private Integer promotionLevel;

        @Schema(description = "积分余额")
        @Min(value = 0, message = "积分不能为负数")
        private Integer points;

        @Schema(description = "积分过期时间")
        private LocalDateTime pointsExpiresAt;
    }

    /**
     * 用户状态更新请求
     */
    @Data
    @Schema(description = "用户状态更新请求")
    public static class UserStatusUpdateRequest {
        @Schema(description = "用户ID")
        @NotNull(message = "用户ID不能为空")
        private Long userId;

        @Schema(description = "是否禁用（0正常/1禁用）")
        @Min(value = 0, message = "状态值不正确")
        @Max(value = 1, message = "状态值不正确")
        private Integer isDeleted;
    }

    /**
     * 用户统计信息
     */
    @Data
    @Schema(description = "用户统计信息")
    public static class UserStatistics {
        @Schema(description = "总用户数")
        private Long totalUsers;

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

        @Schema(description = "本周新增用户数")
        private Long weekNewUsers;

        @Schema(description = "本月新增用户数")
        private Long monthNewUsers;

        @Schema(description = "普通用户数")
        private Long normalUsers;

        // VIP用户详细统计
        @Schema(description = "VIP用户总数")
        private Long vipUsers;

        @Schema(description = "当前有效付费VIP用户数")
        private Long paidVipUsers;

        @Schema(description = "当前有效兑换VIP用户数")
        private Long exchangeVipUsers;

        @Schema(description = "过期VIP用户数")
        private Long expiredVipUsers;

        // SVIP用户详细统计
        @Schema(description = "SVIP用户总数")
        private Long svipUsers;

        @Schema(description = "当前有效付费SVIP用户数")
        private Long paidSvipUsers;

        @Schema(description = "当前有效兑换SVIP用户数")
        private Long exchangeSvipUsers;

        @Schema(description = "过期SVIP用户数")
        private Long expiredSvipUsers;

        // 特殊会员类型统计
        @Schema(description = "赠送会员数（注册2天内的VIP，无付费和兑换记录）")
        private Long giftMembers;

        @Schema(description = "纯兑换会员数（只使用兑换码，从未付费）")
        private Long pureExchangeMembers;

        @Schema(description = "兑换后付费会员数（先兑换后付费）")
        private Long exchangeThenPaidMembers;

        // 认证和推广统计
        @Schema(description = "已实名认证用户数")
        private Long verifiedUsers;

        @Schema(description = "未实名认证用户数")
        private Long unverifiedUsers;

        @Schema(description = "有推广等级用户数")
        private Long promotionUsers;

        // 会员有效性统计
        @Schema(description = "当前有效会员总数（VIP+SVIP，未过期）")
        private Long activeMembersTotal;

        @Schema(description = "过期会员总数")
        private Long expiredMembersTotal;
    }

    /**
     * 用户操作日志
     */
    @Data
    @Schema(description = "用户操作日志")
    public static class UserOperationLog {
        @Schema(description = "操作ID")
        @JsonSerialize(using = ToStringSerializer.class)
        private Long id;

        @Schema(description = "用户ID")
        @JsonSerialize(using = ToStringSerializer.class)
        private Long userId;

        @Schema(description = "操作类型")
        private String operationType;

        @Schema(description = "操作描述")
        private String description;

        @Schema(description = "操作前数据")
        private String beforeData;

        @Schema(description = "操作后数据")
        private String afterData;

        @Schema(description = "操作人ID")
        @JsonSerialize(using = ToStringSerializer.class)
        private Long operatorId;

        @Schema(description = "操作人用户名")
        private String operatorUsername;

        @Schema(description = "操作时间")
        private LocalDateTime createTime;
    }

    /**
     * 用户余额信息
     */
    @Data
    @Schema(description = "用户余额信息")
    public static class BalanceInfo {
        @Schema(description = "当前余额")
        private java.math.BigDecimal balance;

        @Schema(description = "累计总收入")
        private java.math.BigDecimal totalIncome;

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

        @Schema(description = "审核中金额")
        private java.math.BigDecimal frozenAmount;

        @Schema(description = "可用余额")
        private java.math.BigDecimal availableBalance;
    }
}
