package shop.alien.store.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.domain.AntMerchantExpandIndirectZftCreateModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiSort;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import shop.alien.entity.result.R;
import shop.alien.entity.store.dto.AlipayZftBizRequestDto;
import shop.alien.entity.store.dto.AlipayZftMerchantCreateDto;
import shop.alien.entity.store.dto.AlipayZftMerchantSimplecreateDto;
import shop.alien.store.service.AlipayZftOnboardingService;

/**
 * 支付宝直付通二级商户入驻（接口 API 进件）— 对应开放平台
 * ant.merchant.expand.indirect.zft.* / ant.merchant.expand.order.query 等。
 *
 * @see <a href="https://opendocs.alipay.com/solution/0dec7x">支付宝方案文档</a>
 */
@Slf4j
@Api(tags = {"支付宝直付通-二级商户进件"})
@ApiSort(19)
@CrossOrigin
@RestController
@RequestMapping("/store/alipay/zft")
@RequiredArgsConstructor
public class AlipayZftOnboardingController {

    private final AlipayZftOnboardingService alipayZftOnboardingService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "进件前咨询", notes = "ant.merchant.expand.indirect.zft.consult")
    @PostMapping("/consult")
    public R<String> consult(@RequestBody AlipayZftBizRequestDto request) {
        log.info("AlipayZftOnboardingController.consult storeId={}", request != null ? request.getStoreId() : null);
        return alipayZftOnboardingService.zftConsult(request);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "二级商户标准进件", notes = "ant.merchant.expand.indirect.zft.create；业务参数可与官方网关一致放在 body.biz_content（JSON 对象），"
            + "或顶层 external_id 等与 extraBiz 合并；app_auth_token、notify_url、app_id 等同 curl 公共参数")
    @PostMapping("/create")
    public R<String> create(@RequestBody AntMerchantExpandIndirectZftCreateModel request,
                            @RequestParam(value = "storeId", required = false) Integer storeId,
                            @RequestParam(value = "type", required = false) String type) throws AlipayApiException {
        log.info("AlipayZftOnboardingController.create storeId={}", storeId);
        return alipayZftOnboardingService.zftCreate(request, storeId, type);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "二级商户简化进件（免证照）", notes = "ant.merchant.expand.indirect.zft.simplecreate，结构化 body 对应 AntMerchantExpandIndirectZftSimplecreateModel")
    @PostMapping("/simplecreate")
    public R<String> simplecreate(@RequestBody AlipayZftMerchantSimplecreateDto request) {
        log.info("AlipayZftOnboardingController.simplecreate storeId={}", request != null ? request.getStoreId() : null);
        return alipayZftOnboardingService.zftSimplecreate(request);
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "申请单查询", notes = "ant.merchant.expand.order.query；传 orderId（申请单号）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "申请单 id（支付宝 order_id）", required = true, dataType = "string", paramType = "query")
    })
    @PostMapping("/order/query")
    public R<String> orderQuery(@RequestParam String orderId) {
        log.info("AlipayZftOnboardingController.orderQuery orderId={}", orderId);
        return alipayZftOnboardingService.orderQuery(orderId);
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "二级商户信息修改", notes = "ant.merchant.expand.indirect.zft.modify")
    @PostMapping("/modify")
    public R<String> modify(@RequestBody AlipayZftBizRequestDto request) throws AlipayApiException {
        log.info("AlipayZftOnboardingController.modify storeId={}", request != null ? request.getStoreId() : null);
        return alipayZftOnboardingService.zftModify(request);
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "申请单补件", notes = "ant.merchant.expand.indirect.supplement.create")
    @PostMapping("/supplement/create")
    public R<String> supplementCreate(@RequestBody AlipayZftBizRequestDto request) {
        log.info("AlipayZftOnboardingController.supplementCreate storeId={}", request != null ? request.getStoreId() : null);
        return alipayZftOnboardingService.supplementCreate(request);
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "进件图片上传", notes = "ant.merchant.expand.indirect.image.upload")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店ID（取对应支付宝配置）", required = true, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "imageType", value = "图片格式：bmp/jpg/jpeg/png/gif", required = true, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "imageContent", value = "图片文件（二进制，<=10MB）", required = true, dataType = "file", paramType = "form"),
            @ApiImplicitParam(name = "app_id", value = "支付宝应用 app_id；不传则使用门店支付配置", required = false, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "appAuthToken", value = "应用授权令牌 app_auth_token（第三方代调用时选填）", required = false, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "apiVersion", value = "接口版本 api_version（一般无需传）", required = false, dataType = "string", paramType = "form")
    })
    @PostMapping("/image/upload")
    public R<String> imageUpload(@RequestParam(required = false) Integer storeId,
                                 @RequestParam String imageType,
                                 @RequestParam("imageContent") MultipartFile imageContent,
                                 @RequestParam(value = "app_id", required = false) String appId,
                                 @RequestParam(required = false) String appAuthToken,
                                 @RequestParam(required = false) String apiVersion) throws AlipayApiException {
        log.info("AlipayZftOnboardingController.imageUpload storeId={}, imageType={}", storeId, imageType);
        return alipayZftOnboardingService.imageUpload(storeId, imageType, imageContent, appId, appAuthToken, apiVersion);
    }
}
