package com.jeesharp.business.modules.mall.controller;

import com.jeesharp.business.modules.mall.application.MallBargainRecordApplicationService;
import com.jeesharp.business.modules.mall.dto.MallBargainRecordSaveDTO;
import com.jeesharp.business.modules.mall.vo.MallBargainRecordInfoVO;
import com.jeesharp.business.modules.mall.vo.MallBargainRecordListVO;
import com.jeesharp.business.modules.mall.vo.MallBargainRecordQueryVO;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import com.jeesharp.framework.web.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.Map;

/**
 * 砍价记录管理 Controller
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/manager/business/mall/bargainRecord/v1")
@Tag(name = "砍价记录管理")
@AllArgsConstructor
public class MallBargainRecordController extends BaseController {

    private final MallBargainRecordApplicationService mallBargainRecordApplicationService;

    @Operation(summary = "获取详情")
    @GetMapping(value = "info")
    public AjaxResult<MallBargainRecordInfoVO> info(@RequestParam("id") String id) {
        MallBargainRecordInfoVO vo = mallBargainRecordApplicationService.get(id);
        return AjaxResult.okData(vo);
    }

    @Operation(summary = "获取列表（分页）")
    @PostMapping(value = "list")
    public AjaxResult<PageRespVO<MallBargainRecordListVO>> list(@RequestBody MallBargainRecordQueryVO query) {
        PageRespVO<MallBargainRecordListVO> page = mallBargainRecordApplicationService.findPage(new Page<>(query), query);
        return AjaxResult.okData(page);
    }

    @Operation(summary = "保存")
    @PostMapping(value = "save")
    public AjaxResult<Void> save(@RequestBody @Valid MallBargainRecordSaveDTO dto) {
        mallBargainRecordApplicationService.save(dto);
        return AjaxResult.ok("保存成功");
    }

    @Operation(summary = "删除")
    @GetMapping(value = "delete")
    public AjaxResult<Void> delete(@RequestParam("ids") String ids) {
        mallBargainRecordApplicationService.delete(ids);
        return AjaxResult.ok("删除成功");
    }

    @Operation(summary = "检查砍价是否完成")
    @GetMapping(value = "isCompleted")
    public AjaxResult<Boolean> isCompleted(@RequestParam("recordId") String recordId) {
        boolean completed = mallBargainRecordApplicationService.isBargainCompleted(recordId);
        return AjaxResult.okData(completed);
    }

    @Operation(summary = "获取砍价进度")
    @GetMapping(value = "progress")
    public AjaxResult<Map<String, Object>> getProgress(@RequestParam("recordId") String recordId) {
        Map<String, Object> progress = mallBargainRecordApplicationService.calculateBargainProgress(recordId);
        return AjaxResult.okData(progress);
    }

    @Operation(summary = "更新砍价状态")
    @PostMapping(value = "updateStatus")
    public AjaxResult<Void> updateStatus(@RequestParam("recordId") String recordId,
                                  @RequestParam("status") Integer status) {
        mallBargainRecordApplicationService.updateBargainStatus(recordId, status);
        return AjaxResult.ok("更新成功");
    }
}
