package com.erp.skumatch.controller;

import com.erp.common.core.model.ApiResponse;
import com.erp.skumatch.dto.*;
import com.erp.skumatch.entity.Shop;
import com.erp.skumatch.service.SkuMatchService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 平台商品控制器
 */
@Slf4j
@RestController
@RequestMapping("/platform-goods")
@RequiredArgsConstructor
public class PlatformGoodsController {
    
    private final SkuMatchService skuMatchService;
    
    /**
     * 获取平台-店铺树结构
     */
    @GetMapping("/platform-shop/tree")
    public ApiResponse<List<PlatformShopTreeNode>> getPlatformShopTree() {
        return ApiResponse.success(skuMatchService.getPlatformShopTree());
    }
    
    /**
     * 获取店铺列表（兼容旧接口）
     */
    @GetMapping("/shops-by-platform")
    public ApiResponse<List<Shop>> getShopsByPlatform(@RequestParam String platform) {
        return ApiResponse.success(skuMatchService.getShopsByPlatform(platform));
    }
    
    /**
     * 下载平台商品
     */
    @PostMapping("/download")
    public ApiResponse<String> downloadPlatformGoods(@Valid @RequestBody DownloadGoodsRequest request) {
        String taskId = skuMatchService.downloadPlatformGoods(request);
        return ApiResponse.success(taskId);
    }
    
    /**
     * 获取已下载的平台商品列表
     */
    @GetMapping("/list")
    public ApiResponse<List<PlatformGoodsDTO>> getPlatformGoodsList(@RequestParam Long shopId) {
        return ApiResponse.success(skuMatchService.getPlatformGoodsList(shopId));
    }
    
    /**
     * 绑定ERP商品与平台SKU
     */
    @PostMapping("/bind")
    public ApiResponse<Void> bindErpSku(@Valid @RequestBody BindErpSkuRequest request) {
        skuMatchService.bindErpSku(request);
        return ApiResponse.success(null);
    }
}
