package com.dora.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import com.fasterxml.jackson.annotation.JsonFormat;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.DecimalMin;
import jakarta.validation.constraints.DecimalMax;
import jakarta.validation.constraints.Min;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 管理员订单管理相关DTO
 */
public class AdminOrderDto {

    @Data
    @Schema(description = "订单列表请求")
    public static class OrderListRequest {
        @Schema(description = "页码", example = "1")
        private Integer page = 1;

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

        @Schema(description = "订单状态筛选", example = "1")
        private Integer status;

        @Schema(description = "订单类型筛选", example = "membership")
        private String orderType;

        @Schema(description = "搜索关键词", example = "订单号或用户信息")
        private String keyword;

        @Schema(description = "开始日期", example = "2024-01-01")
        private String startDate;

        @Schema(description = "结束日期", example = "2024-01-31")
        private String endDate;

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

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

    @Data
    @Schema(description = "订单列表响应")
    public static class OrderListResponse {
        @Schema(description = "订单列表")
        private List<OrderListItem> list;

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

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

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

    @Data
    @Schema(description = "订单列表项")
    public static class OrderListItem {
        @Schema(description = "订单ID")
        private Long id;

        @Schema(description = "订单号")
        private String orderNo;

        @Schema(description = "用户ID")
        private Long userId;

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

        @Schema(description = "套餐名称")
        private String planName;

        @Schema(description = "原价")
        private BigDecimal originalPrice;

        @Schema(description = "实际支付金额")
        private BigDecimal amount;

        @Schema(description = "订单状态")
        private Integer status;

        @Schema(description = "订单状态名称")
        private String statusName;

        @Schema(description = "支付方式")
        private String paymentMethod;

        @Schema(description = "创建时间")
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private LocalDateTime createTime;

        @Schema(description = "支付时间")
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private LocalDateTime paidAt;
    }

    @Data
    @Schema(description = "订单汇总请求")
    public static class OrderSummaryRequest {
        @Schema(description = "统计周期", example = "daily")
        private String period = "daily";

        @Schema(description = "开始日期", example = "2024-01-01")
        private String startDate;

        @Schema(description = "结束日期", example = "2024-01-31")
        private String endDate;
    }

    @Data
    @Schema(description = "订单汇总响应")
    public static class OrderSummaryResponse {
        @Schema(description = "总收入")
        private BigDecimal totalRevenue;

        @Schema(description = "总订单数")
        private Long totalOrders;

        @Schema(description = "新增VIP会员数")
        private Long newVipUsers;

        @Schema(description = "新增SVIP会员数")
        private Long newSvipUsers;

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

        @Schema(description = "今日订单数")
        private Long todayOrders;

        @Schema(description = "本周收入")
        private BigDecimal weekRevenue;

        @Schema(description = "本月收入")
        private BigDecimal monthRevenue;
    }

    @Data
    @Schema(description = "订单详情响应")
    public static class OrderDetailResponse {
        @Schema(description = "订单基本信息")
        private OrderDetail orderDetail;

        @Schema(description = "用户信息")
        private UserInfo userInfo;

        @Schema(description = "套餐信息")
        private PlanInfo planInfo;
    }

    @Data
    @Schema(description = "订单详细信息")
    public static class OrderDetail {
        @Schema(description = "订单ID")
        private Long id;

        @Schema(description = "订单号")
        private String orderNo;

        @Schema(description = "原价")
        private BigDecimal originalPrice;

        @Schema(description = "实际支付金额")
        private BigDecimal amount;

        @Schema(description = "优惠类型")
        private String discountType;

        @Schema(description = "优惠金额")
        private BigDecimal discountAmount;

        @Schema(description = "优惠描述")
        private String discountDescription;

        @Schema(description = "实际会员时长（天）")
        private Integer actualDurationDays;

        @Schema(description = "订单状态")
        private Integer status;

        @Schema(description = "订单状态名称")
        private String statusName;

        @Schema(description = "支付方式")
        private String paymentMethod;

        @Schema(description = "创建时间")
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private LocalDateTime createTime;

        @Schema(description = "支付时间")
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private LocalDateTime paidAt;
    }

    @Data
    @Schema(description = "用户信息")
    public static class UserInfo {
        @Schema(description = "用户ID")
        private Long userId;

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

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

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

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

    @Data
    @Schema(description = "套餐信息")
    public static class PlanInfo {
        @Schema(description = "套餐ID")
        private Long planId;

        @Schema(description = "套餐名称")
        private String planName;

        @Schema(description = "套餐价格")
        private BigDecimal planPrice;

        @Schema(description = "套餐时长（天）")
        private Integer planDurationDays;

        @Schema(description = "目标角色")
        private Integer targetRole;

        @Schema(description = "目标角色名称")
        private String targetRoleName;
    }

    @Data
    @Schema(description = "订单状态更新请求")
    public static class OrderStatusUpdateRequest {
        @Schema(description = "订单ID")
        private Long orderId;

        @NotNull(message = "状态不能为空")
        @Schema(description = "新状态", example = "1")
        private Integer status;

        @Schema(description = "备注信息")
        private String remark;
    }

    @Data
    @Schema(description = "会员套餐列表请求")
    public static class MembershipPlanListRequest {
        @Schema(description = "页码", example = "1")
        private Integer page = 1;

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

        @Schema(description = "是否仅显示上架套餐")
        private Boolean activeOnly = false;
    }

    @Data
    @Schema(description = "会员套餐列表响应")
    public static class MembershipPlanListResponse {
        @Schema(description = "套餐列表")
        private List<MembershipPlanItem> list;

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

    @Data
    @Schema(description = "会员套餐项")
    public static class MembershipPlanItem {
        @Schema(description = "套餐ID")
        private Long id;

        @Schema(description = "套餐名称")
        private String name;

        @Schema(description = "套餐描述")
        private String description;

        @Schema(description = "价格")
        private BigDecimal price;

        @Schema(description = "时长（天）")
        private Integer durationDays;

        @Schema(description = "目标角色")
        private Integer targetRole;

        @Schema(description = "目标角色名称")
        private String targetRoleName;

        @Schema(description = "优惠百分比率")
        private BigDecimal discountPercentage;

        @Schema(description = "是否上架")
        private Boolean isActive;

        @Schema(description = "创建时间")
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private LocalDateTime createTime;

        @Schema(description = "更新时间")
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private LocalDateTime updateTime;
    }

    @Data
    @Schema(description = "创建会员套餐请求")
    public static class MembershipPlanCreateRequest {
        @NotBlank(message = "套餐名称不能为空")
        @Schema(description = "套餐名称", example = "VIP月卡")
        private String name;

        @Schema(description = "套餐描述")
        private String description;

        @NotNull(message = "价格不能为空")
        @DecimalMin(value = "0.01", message = "价格必须大于0")
        @Schema(description = "价格", example = "29.90")
        private BigDecimal price;

        @NotNull(message = "时长不能为空")
        @Min(value = 1, message = "时长必须大于0")
        @Schema(description = "时长（天）", example = "30")
        private Integer durationDays;

        @NotNull(message = "目标角色不能为空")
        @Schema(description = "目标角色（2VIP/3SVIP）", example = "2")
        private Integer targetRole;

        @DecimalMin(value = "0.00", message = "优惠百分比率不能小于0")
        @DecimalMax(value = "100.00", message = "优惠百分比率不能大于100")
        @Schema(description = "优惠百分比率(0-100)", example = "10.50")
        private BigDecimal discountPercentage = BigDecimal.ZERO;

        @Schema(description = "是否上架", example = "true")
        private Boolean isActive = true;
    }

    @Data
    @Schema(description = "更新会员套餐请求")
    public static class MembershipPlanUpdateRequest {
        @Schema(description = "套餐ID")
        private Long id;

        @NotBlank(message = "套餐名称不能为空")
        @Schema(description = "套餐名称")
        private String name;

        @Schema(description = "套餐描述")
        private String description;

        @NotNull(message = "价格不能为空")
        @DecimalMin(value = "0.01", message = "价格必须大于0")
        @Schema(description = "价格")
        private BigDecimal price;

        @NotNull(message = "时长不能为空")
        @Min(value = 1, message = "时长必须大于0")
        @Schema(description = "时长（天）")
        private Integer durationDays;

        @NotNull(message = "目标角色不能为空")
        @Schema(description = "目标角色")
        private Integer targetRole;

        @DecimalMin(value = "0.00", message = "优惠百分比率不能小于0")
        @DecimalMax(value = "100.00", message = "优惠百分比率不能大于100")
        @Schema(description = "优惠百分比率(0-100)")
        private BigDecimal discountPercentage;

        @Schema(description = "是否上架")
        private Boolean isActive;
    }

    @Data
    @Schema(description = "套餐详情响应")
    public static class MembershipPlanDetailResponse {
        @Schema(description = "套餐详细信息")
        private MembershipPlanItem planDetail;

        @Schema(description = "购买统计")
        private PlanStatistics statistics;
    }

    @Data
    @Schema(description = "套餐统计信息")
    public static class PlanStatistics {
        @Schema(description = "总购买次数")
        private Long totalPurchases;

        @Schema(description = "总收入")
        private BigDecimal totalRevenue;

        @Schema(description = "本月购买次数")
        private Long monthlyPurchases;

        @Schema(description = "本月收入")
        private BigDecimal monthlyRevenue;
    }

    @Data
    @Schema(description = "交易统计请求")
    public static class TransactionStatisticsRequest {
        @Schema(description = "统计周期", example = "daily")
        private String period = "daily";

        @Schema(description = "开始日期", example = "2024-01-01")
        private String startDate;

        @Schema(description = "结束日期", example = "2024-01-31")
        private String endDate;
    }

    @Data
    @Schema(description = "交易统计响应")
    public static class TransactionStatisticsResponse {
        @Schema(description = "总交易金额")
        private BigDecimal totalAmount;

        @Schema(description = "总交易数量")
        private Long totalCount;

        @Schema(description = "成功交易金额")
        private BigDecimal successAmount;

        @Schema(description = "成功交易数量")
        private Long successCount;

        @Schema(description = "失败交易数量")
        private Long failedCount;

        @Schema(description = "退款金额")
        private BigDecimal refundAmount;

        @Schema(description = "退款数量")
        private Long refundCount;

        @Schema(description = "按日期统计")
        private List<DailyStatistics> dailyStats;
    }

    @Data
    @Schema(description = "日交易统计")
    public static class DailyStatistics {
        @Schema(description = "日期")
        private String date;

        @Schema(description = "交易金额")
        private BigDecimal amount;

        @Schema(description = "交易数量")
        private Long count;

        @Schema(description = "新用户数")
        private Long newUsers;
    }
}
