package com.snack.server.module.product.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.snack.server.common.Result;
import com.snack.server.module.product.dto.req.ProductPageReq;
import com.snack.server.module.product.dto.req.ProductSaveReq;
import com.snack.server.module.product.dto.req.StockAdjustReq;
import com.snack.server.module.product.service.ProductService;
import com.snack.server.module.product.vo.ProductDetailVO;
import com.snack.server.module.product.vo.ProductListVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 商品管理（管理端）
 */
@Tag(name = "03. 商品管理")
@RestController
@RequestMapping("/api/admin/product")
@RequiredArgsConstructor
public class ProductAdminController {

    private final ProductService productService;

    @Operation(summary = "分页查询商品")
    @SaCheckLogin
    @GetMapping("/page")
    public Result<Page<ProductListVO>> page(ProductPageReq req) {
        return Result.ok(productService.pageProducts(req));
    }

    @Operation(summary = "商品详情（含 SKU）")
    @SaCheckLogin
    @GetMapping("/{id}")
    public Result<ProductDetailVO> detail(@Parameter(description = "商品 ID") @PathVariable Long id) {
        return Result.ok(productService.getDetail(id));
    }

    @Operation(summary = "发布商品（SPU + SKU）")
    @SaCheckLogin
    @PostMapping
    public Result<Long> create(@Valid @RequestBody ProductSaveReq req) {
        return Result.ok(productService.createProduct(req));
    }

    @Operation(summary = "编辑商品（SPU + SKU）")
    @SaCheckLogin
    @PutMapping
    public Result<Void> update(@Valid @RequestBody ProductSaveReq req) {
        productService.updateProduct(req);
        return Result.ok();
    }

    @Operation(summary = "删除商品（级联删除 SKU）")
    @SaCheckLogin
    @DeleteMapping("/{id}")
    public Result<Void> delete(@Parameter(description = "商品 ID") @PathVariable Long id) {
        productService.deleteProduct(id);
        return Result.ok();
    }

    @Operation(summary = "上下架切换")
    @SaCheckLogin
    @PutMapping("/{id}/status/{status}")
    public Result<Void> updateStatus(
            @Parameter(description = "商品 ID") @PathVariable Long id,
            @Parameter(description = "状态：0-下架 1-上架") @PathVariable Integer status) {
        productService.updateStatus(id, status);
        return Result.ok();
    }

    @Operation(summary = "设置 / 取消热门")
    @SaCheckLogin
    @PutMapping("/{id}/hot/{isHot}")
    public Result<Void> updateHot(
            @PathVariable Long id,
            @Parameter(description = "0-否 1-是") @PathVariable Integer isHot) {
        productService.updateHot(id, isHot);
        return Result.ok();
    }

    @Operation(summary = "设置 / 取消新品")
    @SaCheckLogin
    @PutMapping("/{id}/new/{isNew}")
    public Result<Void> updateNew(
            @PathVariable Long id,
            @Parameter(description = "0-否 1-是") @PathVariable Integer isNew) {
        productService.updateNew(id, isNew);
        return Result.ok();
    }

    @Operation(summary = "调整 SKU 库存")
    @SaCheckLogin
    @PutMapping("/stock/adjust")
    public Result<Void> adjustStock(@Valid @RequestBody StockAdjustReq req) {
        productService.adjustStock(req);
        return Result.ok();
    }
}
