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.RegistrationBatchService;
import com.huimv.employment.service.registration.dto.RegistrationBatchDetailResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 临时工扫码登记批次（企业主进度管理）。
 */
@RestController
@RequestMapping("/api/v1/mp/registration-batches")
@Tag(name = "登记批次", description = "企业主查看扫码登记进度详情，需企业端 JWT")
@SecurityRequirement(name = "Authorization")
public class RegistrationBatchController {

    private static final String USER_TYPE_ENTERPRISE = "enterprise";

    private final RegistrationBatchService registrationBatchService;

    public RegistrationBatchController(RegistrationBatchService registrationBatchService) {
        this.registrationBatchService = registrationBatchService;
    }

    @GetMapping
    @Operation(summary = "查询登记批次详情",
            description = "按草稿 ID 查询进度管理页数据：人数统计、登记进度条、办理流程步骤、人员列表。"
                    + "路径：草稿 → 订单 → 登记批次。仅可查询本企业草稿。",
            parameters = {
                    @Parameter(name = "draft_id", in = ParameterIn.QUERY, required = true,
                            description = "用工草稿 ID（generate / confirm 返回的 draft_id）")
            })
    public R<RegistrationBatchDetailResponse> getDetail(
            @RequestParam("draft_id") Long draftId) {
        LoginUser loginUser = requireEnterprise();
        return R.ok(registrationBatchService.getDetailByDraftId(loginUser.getUserId(), draftId));
    }

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