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.registration.WorkerRegistrationService;
import com.huimv.employment.service.registration.dto.ThirdPartyWorkersAddRequest;
import com.huimv.employment.service.registration.dto.ThirdPartyWorkersAddResponse;
import com.huimv.employment.service.registration.dto.WorkerRegistrationApplyRequest;
import com.huimv.employment.service.registration.dto.WorkerRegistrationApplyResponse;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 临时工批次登记 / 用工邀请申请加入。
 * <p>企业审核、签合同已暂时下线：申请成功即登记完成。</p>
 */
@RestController
@RequestMapping("/api/v1/mp/worker-registration")
@Tag(name = "用工邀请登记", description = "临时工申请加入；企业添加第三方平台工人")
@SecurityRequirement(name = "Authorization")
public class WorkerRegistrationController {

    private static final String USER_TYPE_WORKER = "worker";
    private static final String USER_TYPE_ENTERPRISE = "enterprise";

    private final WorkerRegistrationService workerRegistrationService;

    public WorkerRegistrationController(WorkerRegistrationService workerRegistrationService) {
        this.workerRegistrationService = workerRegistrationService;
    }

    @PostMapping("/apply")
    @Operation(summary = "申请加入用工邀请",
            description = "临时工扫码进入用工邀约后调用。body.scene 为小程序码参数（订单编号）。"
                    + "需已完成档案登记；必传 confirmed_work_type，且须为订单 fe_order_work_item.job_type 之一；"
                    + "无需企业审核、无需签合同，写入 fe_worker_registration（reg_status=completed）。"
                    + "招满时订单可自动进入开工。")
    public R<WorkerRegistrationApplyResponse> apply(@Validated @RequestBody WorkerRegistrationApplyRequest request) {
        LoginUser loginUser = requireWorker();
        return R.ok(workerRegistrationService.apply(loginUser.getUserId(), request));
    }

    @PostMapping("/third-party/add")
    @Operation(summary = "添加第三方平台工人",
            description = "企业端：必传 batch_id 与 workers 列表。"
                    + "写入 fe_worker_registration（third_party_source=1、reg_status=completed）；"
                    + "成功后标记人员登记/招聘完成（订单 current_step_code→task_publish，关闭批次）。"
                    + "workers 项必填：real_name、mobile、id_card_no、confirmed_work_type；"
                    + "手机号与身份证号明文入参，入库加密（并生成 mobile_mask）。")
    public R<ThirdPartyWorkersAddResponse> addThirdPartyWorkers(
            @Validated @RequestBody ThirdPartyWorkersAddRequest request) {
        LoginUser loginUser = requireEnterprise();
        return R.ok(workerRegistrationService.addThirdPartyWorkers(loginUser.getUserId(), request));
    }

    private static LoginUser requireWorker() {
        LoginUser loginUser = LoginUserHolder.require();
        if (!USER_TYPE_WORKER.equals(loginUser.getUserType())) {
            throw new BizException(ErrorCode.WORKER_ONLY);
        }
        return loginUser;
    }

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