package org.dromara.main.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.web.core.BaseController;
import org.dromara.main.domain.bo.MainCompanyApplyBo;
import org.dromara.main.service.IMainCompanyApplyService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 企业入驻门户Controller（公开接口，无需登录）
 *
 * @author sj
 */
@SaIgnore
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/portal/company")
public class PortalCompanyController extends BaseController {

    private final IMainCompanyApplyService mainCompanyApplyService;

    /**
     * 企业入驻申请
     */
    @PostMapping("/apply")
    public R<Void> apply(@RequestBody @Validated(AddGroup.class) MainCompanyApplyBo bo) {
        return toAjax(mainCompanyApplyService.applyCompany(bo));
    }

    /**
     * 检查企业名称是否已存在（前端实时校验用）
     *
     * @param companyName 企业名称
     * @return true=唯一可用，false=已存在
     */
    @GetMapping("/checkName")
    public R<Boolean> checkCompanyName(@RequestParam @NotBlank(message = "企业名称不能为空") String companyName) {
        MainCompanyApplyBo bo = new MainCompanyApplyBo();
        bo.setCompanyName(companyName);
        boolean unique = mainCompanyApplyService.checkCompanyNameUnique(bo);
        return R.ok(unique);
    }

    /**
     * 检查统一社会信用代码是否已存在（前端实时校验用）
     *
     * @param creditCode 统一社会信用代码
     * @return true=唯一可用，false=已存在
     */
    @GetMapping("/checkCreditCode")
    public R<Boolean> checkCreditCode(@RequestParam @NotBlank(message = "统一社会信用代码不能为空") String creditCode) {
        MainCompanyApplyBo bo = new MainCompanyApplyBo();
        bo.setCreditCode(creditCode);
        boolean unique = mainCompanyApplyService.checkCreditCodeUnique(bo);
        return R.ok(unique);
    }

    /**
     * 检查联系人手机号是否已被使用（前端实时校验用）
     *
     * @param contactPhone 联系人手机号
     * @return true=唯一可用，false=已存在
     */
    @GetMapping("/checkPhone")
    public R<Boolean> checkPhone(@RequestParam @NotBlank(message = "手机号不能为空") String contactPhone) {
        MainCompanyApplyBo bo = new MainCompanyApplyBo();
        bo.setMobile(contactPhone);
        boolean unique = mainCompanyApplyService.checkMobileUnique(bo);
        return R.ok(unique);
    }
}

