package com.snack.server.module.product.dto.req;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid;
import jakarta.validation.constraints.*;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * 发布/更新商品请求（含 SKU 列表）
 */
@Data
@Schema(description = "发布/更新商品请求")
public class ProductSaveReq implements Serializable {

    @Schema(description = "商品 ID（更新时必填）")
    private Long id;

    @NotBlank(message = "商品名称不能为空")
    @Size(max = 200, message = "商品名称最长 200 字")
    @Schema(description = "商品名称", example = "原味夏威夷果 500g")
    private String name;

    @NotNull(message = "分类不能为空")
    @Schema(description = "分类 ID", example = "2")
    private Long categoryId;

    @Schema(description = "品牌")
    private String brand;

    @Schema(description = "主图 URL")
    private String mainImage;

    @Schema(description = "副图 JSON 数组字符串")
    private String subImages;

    @Schema(description = "富文本详情（HTML）")
    private String detail;

    @Schema(description = "原价 / 划线价")
    private BigDecimal originPrice;

    @Schema(description = "是否热门：0-否 1-是")
    private Integer isHot = 0;

    @Schema(description = "是否新品：0-否 1-是")
    private Integer isNew = 0;

    @Schema(description = "上架状态：0-下架 1-上架", example = "1")
    private Integer status = 1;

    /**
     * SKU 列表（创建时必填至少 1 个）
     * id 为空 = 新增；非空 = 更新
     */
    @NotNull(message = "SKU 列表不能为空")
    @Size(min = 1, message = "至少需要一个 SKU 规格")
    @Valid
    @Schema(description = "SKU 列表")
    private List<ProductSkuReq> skuList;

    @Data
    public static class ProductSkuReq implements Serializable {

        @Schema(description = "SKU ID（更新时必填）")
        private Long id;

        @NotBlank(message = "规格名称不能为空")
        @Schema(description = "规格名称", example = "原味/500g")
        private String skuName;

        @Schema(description = "规格图片")
        private String image;

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

        @NotNull(message = "库存不能为空")
        @Min(value = 0, message = "库存不能为负数")
        @Schema(description = "库存", example = "500")
        private Integer stock;

        @Schema(description = "重量（克）")
        private Integer weight;

        @Schema(description = "排序号")
        private Integer sort = 0;
    }
}
