package com.erp.product.controller;

import com.erp.product.dto.*;
import com.erp.product.service.GoodsService;
import com.erp.product.exception.GoodsException;
import com.erp.common.core.model.ApiResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/goods")
@RequiredArgsConstructor
public class GoodsController {
    
    private final GoodsService goodsService;
    
    /**
     * 获取商品列表
     */
    @GetMapping
    public ResponseEntity<ApiResponse<GoodsListResponse>> listGoods(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) Long brandId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "15") Integer limit) {
        log.info("查询商品列表: keyword={}, type={}, brandId={}, page={}, limit={}", 
                keyword, type, brandId, page, limit);
        
        GoodsListRequest request = new GoodsListRequest();
        request.setKeyword(keyword);
        request.setType(type);
        request.setBrandId(brandId);
        request.setPage(page);
        request.setLimit(limit);
        
        GoodsListResponse response = goodsService.listGoods(request);
        return ResponseEntity.ok(ApiResponse.success(response));
    }
    
    /**
     * 获取所有商品（用于下拉选择）
     */
    @GetMapping("/all")
    public ResponseEntity<ApiResponse<List<GoodsDTO>>> listAllGoods(
            @RequestParam(required = false) String type) {
        log.info("获取所有商品: type={}", type);
        List<GoodsDTO> goodsList = goodsService.listAllGoods(type);
        return ResponseEntity.ok(ApiResponse.success(goodsList));
    }
    
    /**
     * 获取商品详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<GoodsDTO>> getGoodsById(@PathVariable Long id) {
        log.info("查询商品详情: id={}", id);
        GoodsDTO goods = goodsService.getGoodsById(id);
        return ResponseEntity.ok(ApiResponse.success(goods));
    }
    
    /**
     * 创建商品
     */
    @PostMapping
    public ResponseEntity<ApiResponse<Long>> createGoods(@Valid @RequestBody CreateGoodsRequest request) {
        log.info("创建商品: name={}, type={}", request.getName(), request.getType());
        Long goodsId = goodsService.createGoods(request);
        return ResponseEntity.ok(ApiResponse.success("创建成功", goodsId));
    }
    
    /**
     * 更新商品
     */
    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> updateGoods(
            @PathVariable Long id, 
            @Valid @RequestBody UpdateGoodsRequest request) {
        log.info("更新商品: id={}", id);
        goodsService.updateGoods(id, request);
        return ResponseEntity.ok(ApiResponse.success("更新成功", null));
    }
    
    /**
     * 删除商品
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> deleteGoods(@PathVariable Long id) {
        log.info("删除商品: id={}", id);
        goodsService.deleteGoods(id);
        return ResponseEntity.ok(ApiResponse.success("删除成功", null));
    }
    
    /**
     * 生成商品编码
     */
    @GetMapping("/generate-code")
    public ResponseEntity<ApiResponse<String>> generateCode() {
        log.info("生成商品编码");
        String code = goodsService.generateGoodsCode();
        return ResponseEntity.ok(ApiResponse.success(code));
    }
    
    /**
     * 健康检查接口
     */
    @GetMapping("/health")
    public ResponseEntity<String> health() {
        return ResponseEntity.ok("Goods Service is healthy");
    }
}
