package com.huimv.employment.controller.mp;

import com.huimv.employment.common.exception.BizException;
import com.huimv.employment.common.exception.ErrorCode;
import com.huimv.employment.common.web.R;
import com.huimv.employment.security.LoginUser;
import com.huimv.employment.security.LoginUserHolder;
import com.huimv.employment.service.enterprise.EnterpriseService;
import com.huimv.employment.service.enterprise.dto.EnterpriseInfoResponse;
import com.huimv.employment.service.enterprise.dto.EnterpriseRegisterRequest;
import com.huimv.employment.service.enterprise.dto.EnterpriseRegisterResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 企业登记接口。
 * <p>登录后若 {@code /getInfo} 返回 {@code registered=false}，跳转登记页提交企业基本信息。</p>
 */
@RestController
@RequestMapping("/api/v1/mp/enterprise")
@Tag(name = "企业", description = "首次登录登记，需 JWT 鉴权")
@SecurityRequirement(name = "Authorization")
public class EnterpriseController {

    private static final String USER_TYPE_ENTERPRISE = "enterprise";

    private final EnterpriseService enterpriseService;

    public EnterpriseController(EnterpriseService enterpriseService) {
        this.enterpriseService = enterpriseService;
    }

    @GetMapping("/by-draft/{draftId}")
    @Operation(summary = "按草稿 ID 查询企业信息",
            description = "根据草稿关联的 enterprise_id 返回企业详情。"
                    + "含法人身份证号明文 legal_person_id_card、未脱敏手机号 contact_mobile。"
                    + "仅可查询本人草稿；草稿未绑企业时返回错误。")
    public R<EnterpriseInfoResponse> getByDraftId(@PathVariable("draftId") Long draftId) {
        LoginUser loginUser = requireEnterprise();
        return R.ok(enterpriseService.getByDraftId(loginUser.getUserId(), draftId));
    }

    @PostMapping("/register")
    @Operation(summary = "提交企业登记/认证",
            description = "必填 draftId。按 credit_code 存在则更新、不存在则插入企业；"
                    + "按当前登录用户 upsert fe_enterprise_user，认证成功后设为默认企业并取消该用户其它默认；"
                    + "回填该草稿及对应订单、登记批次的 enterprise_id。")
    public R<EnterpriseRegisterResponse> register(@Validated @RequestBody EnterpriseRegisterRequest request) {
        LoginUser loginUser = requireEnterprise();
        return R.ok(enterpriseService.register(loginUser.getUserId(), request));
    }

    private static LoginUser requireEnterprise() {
        LoginUser loginUser = LoginUserHolder.require();
        if (!USER_TYPE_ENTERPRISE.equals(loginUser.getUserType())) {
            throw new BizException(ErrorCode.ENTERPRISE_ONLY);
        }
        return loginUser;
    }
}
