package co.yixiang.yshop.module.order.controller.app.afterorder;

import co.yixiang.yshop.framework.common.pojo.CommonResult;
import co.yixiang.yshop.framework.security.core.annotations.PreAuthenticated;
import co.yixiang.yshop.module.order.controller.app.afterorder.param.AppStoreAfterSalesParam;
import co.yixiang.yshop.module.order.controller.app.afterorder.vo.AppStoreAfterSalesVo;
import co.yixiang.yshop.module.order.controller.app.afterorder.vo.AppStoreOrderCartInfoVo;
import co.yixiang.yshop.module.order.service.storeaftersales.AppStoreAfterSalesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;
import static co.yixiang.yshop.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * @author : yshop 2023/6/23 16:00
 */
@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Tag(name = "用户 APP - 订单售后")
@RequestMapping("/after")
public class AppOrderAfterSalesController {

    private final AppStoreAfterSalesService appStoreAfterSalesService;

    @PreAuthenticated
    @PostMapping("/applyForAfterSales")
    @Operation(summary = "申请售后")
    public CommonResult<Boolean> applyForAfterSales(@RequestBody AppStoreAfterSalesParam storeAfterSalesParam) {
        Long uid = getLoginUserId();
        appStoreAfterSalesService.applyForAfterSales(uid, storeAfterSalesParam);
        return success(true);
    }

    @PreAuthenticated
    @GetMapping("/applyForAfterSales/{key}")
    @Operation(summary = "查询订单详情")
    @Parameter(name = "key", description = "唯一的uni值或者订单号", required = true, example = "10      ")
    public CommonResult<List<AppStoreOrderCartInfoVo>> checkOrderDetails(@PathVariable String key) {
        return success(appStoreAfterSalesService.checkOrderDetails(key));
    }


    @PreAuthenticated
    @GetMapping("/storeAfterSales/list")
    @Operation(summary = "售后列表")
    @Parameters({
            @Parameter(name = "type", description = "商品状态,-1全部 1售后中 2 已完成",
                    required = true, example = "1"),
            @Parameter(name = "page", description = "页码,默认为1",
                    required = true, example = "1"),
            @Parameter(name = "limit", description = "页大小,默认为10",
                    required = true, example = "10      ")
    })
    public CommonResult<List<AppStoreAfterSalesVo>> salesList(@RequestParam(value = "type", defaultValue = "0") int type,
                                                              @RequestParam(value = "page", defaultValue = "1") int page,
                                                              @RequestParam(value = "limit", defaultValue = "10") int limit) {
        Long uid = getLoginUserId();
        return success(appStoreAfterSalesService.salesList(uid, type, page, null, limit));
    }


    @PreAuthenticated
    @GetMapping("/store/detail/{key}/{id}")
    @Parameters({
            @Parameter(name = "key", description = "唯一的uni值或者订单号", required = true, example = "10      "),
            @Parameter(name = "id", description = "售后id", required = true, example = "1")
    })
    @Operation(summary = "查看售後详情")
    public CommonResult<AppStoreAfterSalesVo> detail(@PathVariable String key, @PathVariable Long id) {
        Long uid = getLoginUserId();
        AppStoreAfterSalesVo storeInfo = appStoreAfterSalesService.getStoreInfoByOrderCodeAndAfterIdAndUid(key, id, uid);
        return success(storeInfo);
    }

    @PreAuthenticated
    @GetMapping("/store/detail/{key}")
    @Parameter(name = "key", description = "唯一的uni值或者订单号", required = true, example = "10      ")
    @Operation(summary = "通过订单号搜索")
    public CommonResult<List<AppStoreAfterSalesVo>> detail(@PathVariable String key) {
        Long uid = getLoginUserId();
        return success(appStoreAfterSalesService.getStoreInfoByOrderCodeAndUid(key, uid));
    }

    @PreAuthenticated
    @GetMapping("/revoke/{key}/{id}")
    @Parameter(name = "key", description = "唯一的uni值或者订单号", required = true, example = "10      ")
    @Operation(summary = "撤销申请")
    public CommonResult<Boolean> revoke(@PathVariable String key, @PathVariable Long id) {
        Long uid = getLoginUserId();
        Boolean revoke = appStoreAfterSalesService.revoke(key, uid, id);
        return success(revoke);
    }
//
//    @PreAuthenticated
//    @GetMapping(value = "/yxExpress")
//    @Operation(summary = "查询快递")
//    public ApiResult<List<YxExpress>> getYxExpresss(YxExpressQueryCriteria criteria) {
//        return ApiResult.ok(yxExpressService.queryAll(criteria));
//    }


    @PreAuthenticated
    @PostMapping("/addLogisticsInformation")
    @Parameters({
            @Parameter(name = "code", description = "快递公司编码", required = true),
            @Parameter(name = "name", description = "快递公司名称", required = true),
            @Parameter(name = "postalCode", description = "快递编号", required = true),
            @Parameter(name = "orderCode", description = "订单编号", required = true),
            @Parameter(name = "id", description = "售后订单id", required = true, example = "1")
    })
    @Operation(summary = "添加物流信息")
    public CommonResult<Boolean> addLogisticsInformation(@RequestParam String code, @RequestParam String name,
                                                      @RequestParam String postalCode, @RequestParam String orderCode,
                                                      @RequestParam Long id) {
        Boolean result = appStoreAfterSalesService.addLogisticsInformation(code, name, postalCode, orderCode, id);
        return success(result);
    }



    @PreAuthenticated
    @DeleteMapping("/deleteAfterSalesOrder")
    @Parameters({
            @Parameter(name = "orderCode", description = "订单编号", required = true),
            @Parameter(name = "id", description = "售后订单id", required = true, example = "1")
    })
    @Operation(summary = "删除售后订单")
    public CommonResult<Boolean> deleteAfterSalesOrder(@RequestParam String orderCode, @RequestParam Long id) {
        Boolean result = appStoreAfterSalesService.deleteAfterSalesOrder(orderCode, id);
        return success(result);
    }


}
