package com.erp.order.dto.response;

import com.erp.order.entity.Order;
import com.erp.order.entity.OrderItem;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.Builder;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Data
@Builder
public class OrderResponse {

    private Long id;
    private String shortId;
    private String platformOrderSn;
    private String platform;
    private Long shopId;
    private String shopName;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime orderTime;

    private String buyerNick;
    private String receiverName;
    private String receiverPhone;
    private String receiverAddress;
    private BigDecimal goodsAmount;
    private BigDecimal discountAmount;
    private BigDecimal freight;
    private BigDecimal totalAmount;
    private String orderStatus;
    private String orderStatusText;
    private String platformStatus;
    private String auditStatus;
    private String auditStatusText;
    private String deliveryStatus;
    private String deliveryStatusText;
    private Long warehouseId;
    private String warehouseName;
    private String expressCompany;
    private String expressName;
    private String expressNo;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime deliveryTime;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime endTime;

    private String auditComment;
    private String remark;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createdAt;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updatedAt;

    private List<OrderItemResponse> items;

    public static OrderResponse fromEntity(Order order) {
        return OrderResponse.builder()
                .id(order.getId())
                .shortId(order.getShortId())
                .platformOrderSn(order.getPlatformOrderSn())
                .platform(order.getPlatform())
                .shopId(order.getShopId())
                .shopName(order.getShopName())
                .orderTime(order.getOrderTime())
                .buyerNick(order.getBuyerNick())
                .receiverName(order.getReceiverName())
                .receiverPhone(order.getReceiverPhone())
                .receiverAddress(order.getReceiverAddress())
                .goodsAmount(order.getGoodsAmount())
                .discountAmount(order.getDiscountAmount())
                .freight(order.getFreight())
                .totalAmount(order.getTotalAmount())
                .orderStatus(order.getOrderStatus())
                .orderStatusText(getOrderStatusText(order.getOrderStatus()))
                .platformStatus(order.getPlatformStatus())
                .auditStatus(order.getAuditStatus())
                .auditStatusText(getAuditStatusText(order.getAuditStatus()))
                .deliveryStatus(order.getDeliveryStatus())
                .deliveryStatusText(getDeliveryStatusText(order.getDeliveryStatus()))
                .warehouseId(order.getWarehouseId())
                .warehouseName(order.getWarehouseName())
                .expressCompany(order.getExpressCompany())
                .expressName(order.getExpressName())
                .expressNo(order.getExpressNo())
                .deliveryTime(order.getDeliveryTime())
                .endTime(order.getEndTime())
                .auditComment(order.getAuditComment())
                .remark(order.getRemark())
                .createdAt(order.getCreatedAt())
                .updatedAt(order.getUpdatedAt())
                .items(order.getItems() != null
                        ? order.getItems().stream().map(OrderItemResponse::fromEntity).collect(Collectors.toList())
                        : null)
                .build();
    }

    public static String getOrderStatusText(String status) {
        if (status == null) return "";
        switch (status) {
            case "pending": return "待处理";
            case "auditing": return "审核中";
            case "shipped": return "已发货";
            case "completed": return "已完成";
            case "cancelled": return "已取消";
            default: return status;
        }
    }

    public static String getAuditStatusText(String status) {
        if (status == null) return "";
        switch (status) {
            case "pending": return "待审核";
            case "approved": return "已通过";
            case "rejected": return "已驳回";
            default: return status;
        }
    }

    public static String getDeliveryStatusText(String status) {
        if (status == null) return "";
        switch (status) {
            case "pending": return "待发货";
            case "delivered": return "已发货";
            default: return status;
        }
    }
}
