package cn.edu.ecnu.stu.bookstore.controller;

import cn.edu.ecnu.stu.bookstore.component.AppException;
import cn.edu.ecnu.stu.bookstore.component.Constants;
import cn.edu.ecnu.stu.bookstore.component.Result;
import cn.edu.ecnu.stu.bookstore.pojo.Book;
import cn.edu.ecnu.stu.bookstore.pojo.Store;
import cn.edu.ecnu.stu.bookstore.service.SellerService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/seller")
public class SellerController {

    @Autowired
    public SellerService sellerService;

    @PostMapping("/create_store")
    public Result createStore(@RequestBody Store store) {
        sellerService.createStore(store);
        return Result.success();
    }

    @PostMapping("add_book")
    public Result addBook(@RequestBody Book book) {
        sellerService.addBook(book);
        return Result.success();
    }

    @PostMapping("add_stock_level")
    public Result addStockLevel(@RequestBody JSONObject body) {
        String storeId = body.getString("storeId");
        String bookId = body.getString("bookId");
        int addStockLevel = body.getIntValue("addStockLevel");
        sellerService.addStockLevel(storeId, bookId, addStockLevel);
        return Result.success();
    }

    @PostMapping("send_goods")
    public Result sendGoods(@RequestBody Map<String, String> body) {
        String orderId = body.get("orderId");
        if(!StringUtils.hasText(orderId))
            throw new AppException(Constants.CLIENT_ERROR, Constants.PARAMETER_ERROR_MESSAGE);
        sellerService.sendGoods(orderId);
        return Result.success();
    }
}
