import { post } from '@/utils/request.js'

/**
 * 企业端：添加第三方平台工人
 * POST /api/v1/mp/worker-registration/third-party/add
 * @param {{
 *   batchId: number|string,
 *   workers: Array<{
 *     workerId?: number,
 *     realName: string,
 *     mobile: string,
 *     idCardNo: string,
 *     confirmedWorkType: string
 *   }>
 * }} data
 */
export function addThirdPartyWorkers(data = {}, options = {}) {
	const batchId = data.batchId != null ? data.batchId : data.batch_id
	const workers = Array.isArray(data.workers) ? data.workers : []
	return post(
		'/worker-registration/third-party/add',
		{
			batch_id: batchId,
			batchId,
			workers
		},
		options
	)
}

/**
 * 申请加入用工邀请
 * POST /api/v1/mp/worker-registration/apply
 * 需临时工 JWT；body.scene 为小程序码 / qrToken（订单编号）
 * @param {{ scene: string, confirmedWorkType?: string }} data
 */
export function applyWorkerRegistration(data = {}, options = {}) {
	const scene = data.scene == null ? '' : String(data.scene).trim()
	const confirmedWorkType = data.confirmedWorkType == null && data.confirmed_work_type == null
		? ''
		: String(data.confirmedWorkType || data.confirmed_work_type || '').trim()
	const body = { scene }
	if (confirmedWorkType) {
		body.confirmedWorkType = confirmedWorkType
		body.confirmed_work_type = confirmedWorkType
	}
	return post('/worker-registration/apply', body, options)
}

/**
 * 企业审核通过用工登记
 * POST /api/v1/mp/worker-registration/{id}/approve
 * @param {number|string} registrationId fe_worker_registration.id
 */
export function approveWorkerRegistration(registrationId, options = {}) {
	return post(`/worker-registration/${registrationId}/approve`, {}, options)
}

/**
 * 企业驳回用工登记
 * POST /api/v1/mp/worker-registration/{id}/reject
 * @param {number|string} registrationId
 * @param {{ failReason?: string, fail_reason?: string, reason?: string }} data 驳回原因必填
 */
export function rejectWorkerRegistration(registrationId, data = {}, options = {}) {
	const failReason = String(
		data.failReason || data.fail_reason || data.reason || ''
	).trim()
	return post(
		`/worker-registration/${registrationId}/reject`,
		{ fail_reason: failReason, failReason },
		options
	)
}
