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;

/**
 * 商品控制器 - 租户后台API
 * 路由: /api/products
 */
@Slf4j
@RestController
@RequestMapping("/api/products")
@RequiredArgsConstructor
public class ProductController {
    
    private final GoodsService goodsService;
    
    /**
     * 获取商品列表
     * GET /api/products
     */
    @GetMapping
    public ResponseEntity<ApiResponse<GoodsListResponse>> listProducts(
            @RequestParam(required = false) Long tenantId,
            @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("查询商品列表: tenantId={}, keyword={}, type={}, brandId={}, page={}, limit={}", 
                tenantId, 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));
    }
    
    /**
     * 获取所有商品（用于下拉选择）
     * GET /api/products/all
     */
    @GetMapping("/all")
    public ResponseEntity<ApiResponse<List<GoodsDTO>>> listAllProducts(
            @RequestParam(required = false) Long tenantId,
            @RequestParam(required = false) String type) {
        log.info("获取所有商品: tenantId={}, type={}", tenantId, type);
        List<GoodsDTO> goodsList = goodsService.listAllGoods(type);
        return ResponseEntity.ok(ApiResponse.success(goodsList));
    }
    
    /**
     * 获取商品详情
     * GET /api/products/{id}
     */
    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<GoodsDTO>> getProductById(
            @RequestParam(required = false) Long tenantId,
            @PathVariable Long id) {
        log.info("查询商品详情: tenantId={}, id={}", tenantId, id);
        GoodsDTO goods = goodsService.getGoodsById(id);
        return ResponseEntity.ok(ApiResponse.success(goods));
    }
    
    /**
     * 创建商品
     * POST /api/products
     */
    @PostMapping
    public ResponseEntity<ApiResponse<Long>> createProduct(
            @RequestParam(required = false) Long tenantId,
            @Valid @RequestBody CreateGoodsRequest request) {
        log.info("创建商品: tenantId={}, name={}, type={}", tenantId, request.getName(), request.getType());
        Long goodsId = goodsService.createGoods(request);
        return ResponseEntity.ok(ApiResponse.success("创建成功", goodsId));
    }
    
    /**
     * 更新商品
     * PUT /api/products/{id}
     */
    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> updateProduct(
            @RequestParam(required = false) Long tenantId,
            @PathVariable Long id, 
            @Valid @RequestBody UpdateGoodsRequest request) {
        log.info("更新商品: tenantId={}, id={}", tenantId, id);
        goodsService.updateGoods(id, request);
        return ResponseEntity.ok(ApiResponse.success("更新成功", null));
    }
    
    /**
     * 删除商品
     * DELETE /api/products/{id}
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> deleteProduct(
            @RequestParam(required = false) Long tenantId,
            @PathVariable Long id) {
        log.info("删除商品: tenantId={}, id={}", tenantId, id);
        goodsService.deleteGoods(id);
        return ResponseEntity.ok(ApiResponse.success("删除成功", null));
    }
    
    /**
     * 生成商品编码
     * GET /api/products/generate-code
     */
    @GetMapping("/generate-code")
    public ResponseEntity<ApiResponse<String>> generateCode(@RequestParam(required = false) Long tenantId) {
        log.info("生成商品编码: tenantId={}", tenantId);
        String code = goodsService.generateGoodsCode();
        return ResponseEntity.ok(ApiResponse.success(code));
    }
    
    /**
     * 健康检查接口
     */
    @GetMapping("/health")
    public ResponseEntity<String> health() {
        return ResponseEntity.ok("Product Service is healthy");
    }
}
