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

import com.jeesharp.business.modules.mall.application.MallFreightTemplateApplicationService;
import com.jeesharp.business.modules.mall.dto.MallFreightTemplateSaveDTO;
import com.jeesharp.business.modules.mall.entity.MallFreightTemplate;
import com.jeesharp.business.modules.mall.vo.MallFreightTemplateInfoVO;
import com.jeesharp.business.modules.mall.vo.MallFreightTemplateListVO;
import com.jeesharp.business.modules.mall.vo.MallFreightTemplateQueryVO;
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.math.BigDecimal;
import java.util.List;

/**
 * 运费模板管理 Controller
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/manager/business/mall/freightTemplate/v1")
@Tag(name = "运费模板管理")
@AllArgsConstructor
public class MallFreightTemplateController extends BaseController {

    private final MallFreightTemplateApplicationService mallFreightTemplateApplicationService;

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

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

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

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

    @Operation(summary = "获取所有启用模板")
    @GetMapping(value = "getEnabled")
    public AjaxResult<List<MallFreightTemplate>> getEnabled() {
        List<MallFreightTemplate> templates = mallFreightTemplateApplicationService.getEnabledTemplates();
        return AjaxResult.okData(templates);
    }

    @Operation(summary = "计算运费")
    @PostMapping(value = "calculate")
    public AjaxResult<BigDecimal> calculate(@RequestParam("templateId") String templateId,
                               @RequestParam(value = "provinceCode", required = false) String provinceCode,
                               @RequestParam(value = "cityCode", required = false) String cityCode,
                               @RequestParam(value = "districtCode", required = false) String districtCode,
                               @RequestParam(value = "province", required = false) String province,
                               @RequestParam(value = "city", required = false) String city,
                               @RequestParam(value = "district", required = false) String district,
                               @RequestParam(value = "totalWeight", required = false) BigDecimal totalWeight,
                               @RequestParam(value = "totalQuantity", required = false) Integer totalQuantity,
                               @RequestParam(value = "orderAmount", required = false) BigDecimal orderAmount) {
        BigDecimal freight = mallFreightTemplateApplicationService.calculateFreight(templateId,
            provinceCode, cityCode, districtCode, province, city, district,
            totalWeight, totalQuantity, orderAmount);
        return AjaxResult.okData(freight);
    }
}
