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

import com.jeesharp.business.modules.mall.application.MallBargainActivityApplicationService;
import com.jeesharp.business.modules.mall.application.MallBargainHelpApplicationService;
import com.jeesharp.business.modules.mall.application.MallBargainRecordApplicationService;
import com.jeesharp.business.modules.mall.entity.MallBargainActivity;
import com.jeesharp.business.modules.mall.entity.MallBargainRecord;
import com.jeesharp.business.modules.mall.vo.MallBargainRecordInfoVO;
import com.jeesharp.business.modules.mall.vo.MallBargainHelpInfoVO;
import com.jeesharp.business.modules.mall.vo.MallBargainRecordListVO;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
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 java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 砍价移动端 Controller
 */
@RestController
@RequestMapping(value = "${jeesharp.web.mobilePath}/mall/bargain/v1")
@Tag(name = "砍价功能")
@AllArgsConstructor
public class MallBargainMobileController {

    private final MallBargainActivityApplicationService activityApplicationService;
    private final MallBargainRecordApplicationService recordApplicationService;
    private final MallBargainHelpApplicationService helpApplicationService;

    @Operation(summary = "获取进行中的砍价活动")
    @GetMapping(value = "activities")
    public AjaxResult<List<MallBargainActivity>> getActiveActivities() {
        List<MallBargainActivity> activities = activityApplicationService.getActiveActivities();
        return AjaxResult.okData(activities);
    }

    @Operation(summary = "根据SKU获取砍价活动")
    @GetMapping(value = "getBySku")
    public AjaxResult<MallBargainActivity> getBySku(@RequestParam("skuId") String skuId) {
        MallBargainActivity activity = activityApplicationService.getBySkuId(skuId);
        return AjaxResult.okData(activity);
    }

    @Operation(summary = "发起砍价")
    @PostMapping(value = "start")
    public AjaxResult<String> startBargain(@RequestParam("activityId") String activityId,
                                  @RequestParam("memberId") String memberId) {
        try {
            String recordId = recordApplicationService.startBargain(activityId, memberId);
            return AjaxResult.okData(recordId);
        } catch (Exception e) {
            return AjaxResult.failMsg(e.getMessage());
        }
    }

    @Operation(summary = "获取砍价详情")
    @GetMapping(value = "detail")
    public AjaxResult<MallBargainRecordInfoVO> getDetail(@RequestParam("recordId") String recordId) {
        MallBargainRecordInfoVO record = recordApplicationService.get(recordId);
        if (record != null) {
            // 获取助力列表
            List<MallBargainHelpInfoVO> helps = helpApplicationService.getBargainHelps(recordId);
            record.setHelpList(helps);
        }
        return AjaxResult.okData(record);
    }

    @Operation(summary = "获取用户砍价记录")
    @GetMapping(value = "myRecords")
    public AjaxResult<PageRespVO<MallBargainRecordListVO>> getMyRecords(@RequestParam("memberId") String memberId,
                                  @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageRespVO<MallBargainRecordListVO> page = recordApplicationService.getUserBargainRecords(
                memberId, new Page<MallBargainRecord>(pageNo.longValue(), pageSize.longValue()));
        return AjaxResult.okData(page);
    }

    @Operation(summary = "好友助力")
    @PostMapping(value = "help")
    public AjaxResult<Map<String, Object>> helpBargain(@RequestParam("recordId") String recordId,
                                 @RequestParam("memberId") String memberId,
                                 @RequestParam(value = "helpType", defaultValue = "0") Integer helpType) {
        try {
            BigDecimal cutAmount = helpApplicationService.helpBargain(recordId, memberId, helpType);
            Map<String, Object> result = Map.of(
                "cutAmount", cutAmount,
                "message", "助力成功，砍掉" + cutAmount + "元"
            );
            return AjaxResult.okData(result);
        } catch (Exception e) {
            return AjaxResult.failMsg(e.getMessage());
        }
    }

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

    @Operation(summary = "检查是否可下单")
    @GetMapping(value = "canOrder")
    public AjaxResult<Boolean> canOrder(@RequestParam("recordId") String recordId) {
        boolean canOrder = recordApplicationService.isBargainCompleted(recordId);
        return AjaxResult.okData(canOrder);
    }
}