package com.ycwl.basic.controller.pc;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ycwl.basic.model.pc.price.entity.PriceConfigEntity;
import com.ycwl.basic.model.pc.price.req.PriceConfigListReq;
import com.ycwl.basic.model.pc.price.resp.GoodsListRespVO;
import com.ycwl.basic.model.pc.price.resp.PriceConfigRespVO;
import com.ycwl.basic.repository.PriceRepository;
import com.ycwl.basic.service.pc.PriceConfigService;
import com.ycwl.basic.utils.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/priceConfig/v1")
public class PriceConfigController {

    @Autowired
    private PriceConfigService priceConfigService;
    @Autowired
    private PriceRepository priceRepository;

    @GetMapping("/goodsList")
    public ApiResponse<List<GoodsListRespVO>> goodsList(@RequestParam Long scenicId) {
        return ApiResponse.success(priceConfigService.listGoodsByScenic(scenicId));
    }

    @PostMapping("/add")
    public ApiResponse<PriceConfigEntity> addPriceConfig(@RequestBody PriceConfigEntity priceConfig) {
        priceConfig.setId(null);
        priceConfigService.save(priceConfig);
        return ApiResponse.success(priceConfig);
    }

    @PostMapping("/update")
    public ApiResponse<PriceConfigEntity> updatePriceConfig(@RequestBody PriceConfigEntity priceConfig) {
        priceRepository.clearPriceCache(priceConfig.getId());
        priceConfigService.updateById(priceConfig);
        priceRepository.clearPriceCache(priceConfig.getId());
        return ApiResponse.success(priceConfig);
    }

    @DeleteMapping("/delete/{id}")
    public ApiResponse<Boolean> deletePriceConfig(@PathVariable Integer id) {
        priceRepository.clearPriceCache(id);
        priceConfigService.removeById(id);
        priceRepository.clearPriceCache(id);
        return ApiResponse.success(true);
    }

    @PostMapping("/{id}/status")
    public ApiResponse<Boolean> updateStatus(@PathVariable Integer id) {
        priceRepository.clearPriceCache(id);
        priceConfigService.updateStatus(id);
        priceRepository.clearPriceCache(id);
        return ApiResponse.success(true);
    }

    @GetMapping("/{id}")
    public ApiResponse<PriceConfigRespVO> getPriceConfigById(@PathVariable Integer id) {
        PriceConfigRespVO config = priceConfigService.findById(id);
        priceConfigService.fillGoodsName(config);
        return ApiResponse.success(config);
    }

    @GetMapping("/list")
    public ApiResponse<PageInfo<PriceConfigRespVO>> list(@RequestParam(defaultValue = "1") int pageNum,
                                                         @RequestParam(defaultValue = "10") int pageSize,
                                                         @ModelAttribute PriceConfigListReq req) {
        PageHelper.startPage(pageNum, pageSize);
        List<PriceConfigRespVO> result = priceConfigService.listByCondition(req);
        priceConfigService.fillGoodsName(result);
        PageInfo<PriceConfigRespVO> pageInfo = new PageInfo<>(result);
        return ApiResponse.success(pageInfo);
    }
}