package cn.newfeifan.mall.module.sale.controller.app.merchantapply;

import cn.newfeifan.mall.framework.security.core.annotations.PreAuthenticated;
import cn.newfeifan.mall.module.sale.controller.app.merchantapply.vo.MerchantApplyRespVO;
import cn.newfeifan.mall.module.sale.controller.app.merchantapply.vo.MerchantApplySaveReqVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import cn.newfeifan.mall.framework.common.pojo.CommonResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;
import static cn.newfeifan.mall.framework.common.pojo.CommonResult.success;
import static cn.newfeifan.mall.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

import cn.newfeifan.mall.module.sale.dal.dataobject.merchantapply.MerchantApplyDO;
import cn.newfeifan.mall.module.sale.service.merchantapply.MerchantApplyService;

import java.util.ArrayList;
import java.util.List;

@Tag(name = "管理后台 - 商户申请")
@RestController
@RequestMapping("/sale/merchant-apply")
@Validated
public class MerchantApplyController {

    @Resource
    private MerchantApplyService merchantApplyService;

    @PostMapping("/create")
    @Operation(summary = "创建商户申请")
    @PreAuthenticated
    public CommonResult<Long> createMerchantApply(@Valid @RequestBody MerchantApplySaveReqVO createReqVO) {
        return success(merchantApplyService.createMerchantApply(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商户申请")
    @PreAuthenticated
    public CommonResult<Boolean> updateMerchantApply(@Valid @RequestBody MerchantApplySaveReqVO updateReqVO) {
        merchantApplyService.updateMerchantApply(updateReqVO);
        return success(true);
    }

//    @DeleteMapping("/delete")
//    @Operation(summary = "删除商户申请")
//    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthenticated
//    public CommonResult<Boolean> deleteMerchantApply(@RequestParam("id") Long id) {
//        merchantApplyService.deleteMerchantApply(id);
//        return success(true);
//    }

    @GetMapping("/get")
    @Operation(summary = "获得商户申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthenticated
    public CommonResult<MerchantApplyRespVO> getMerchantApply() {
        MerchantApplyDO merchantApply = merchantApplyService.getMerchantApplyBYUserId(getLoginUserId());
        MerchantApplyRespVO bean = BeanUtils.toBean(merchantApply, MerchantApplyRespVO.class);

        if(merchantApply != null){
            // Step 1: 去除字符串中的方括号和空格
            String input = merchantApply.getOtherCertificate().replaceAll("[\\[\\]\\s]", "");

            // Step 2: 按逗号分割字符串
            String[] parts = input.split(",");

            List<String> fileList = new ArrayList<>();

            // Step 3: 去除每个部分可能的空格并添加到列表中
            for (String part : parts) {
                fileList.add(part.trim());
            }

            bean.setOtherCertificate(fileList);
        }

        return success(bean);
    }

//    @GetMapping("/page")
//    @Operation(summary = "获得商户申请分页")
//    @PreAuthenticated
//    public CommonResult<PageResult<MerchantApplyRespVO>> getMerchantApplyPage(@Valid MerchantApplyPageReqVO pageReqVO) {
//        PageResult<MerchantApplyDO> pageResult = merchantApplyService.getMerchantApplyPage(pageReqVO);
//        return success(BeanUtils.toBean(pageResult, MerchantApplyRespVO.class));
//    }

    @GetMapping("/isSystemUserByMobile")
    @Operation(summary = "判断是否是系统用户并且没有商户申请")
    @PreAuthenticated
    public CommonResult<Boolean> isSystemUserByMobile() {
        return success(merchantApplyService.isSystemUserByMobile());
    }
}