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

/**
 * 查询开工就绪状态
 * GET /api/v1/mp/orders/{orderId}/start-work-status
 */
export function getStartWorkStatus(orderId, options = {}) {
	return get(`/orders/${orderId}/start-work-status`, {}, options)
}

/**
 * 确认开工
 * POST /api/v1/mp/orders/{orderId}/start-work
 * @param {{ allowUnderstaffed?: boolean }} [data]
 */
export function startWork(orderId, data = {}, options = {}) {
	const allow = !!(data.allowUnderstaffed || data.allow_understaffed)
	return post(
		`/orders/${orderId}/start-work`,
		{ allow_understaffed: allow, allowUnderstaffed: allow },
		options
	)
}

/**
 * 查询任务发布详情
 * GET /api/v1/mp/orders/{orderId}/publish-task
 * @param {number|string} orderId
 */
export function getOrderPublishTask(orderId, options = {}) {
	return get(`/orders/${orderId}/publish-task`, {}, options)
}

/**
 * 任务发布
 * POST /api/v1/mp/orders/{orderId}/publish-task
 * Body:
 * {
 *   work_items: [{ id?, group_no?, job_type?, work_start_date, work_end_date }],
 *   allow_understaffed?: boolean
 * }
 * @param {number|string} orderId
 * @param {Object} data TaskPublishRequest
 */
export function publishOrderTask(orderId, data = {}, options = {}) {
	return post(`/orders/${orderId}/publish-task`, data || {}, options)
}

/**
 * 发起结算
 * POST /api/v1/mp/orders/{orderId}/start-settlement
 * @param {{ allowBeforeEndDate?: boolean }} [data]
 */
export function startSettlement(orderId, data = {}, options = {}) {
	const allow = !!(data.allowBeforeEndDate || data.allow_before_end_date)
	return post(
		`/orders/${orderId}/start-settlement`,
		{ allow_before_end_date: allow, allowBeforeEndDate: allow },
		options
	)
}

/**
 * 查询订单结算单
 * GET /api/v1/mp/orders/{orderId}/settlement
 */
export function getOrderSettlement(orderId, options = {}) {
	return get(`/orders/${orderId}/settlement`, {}, options)
}

/**
 * 确认结算并支付
 * POST /api/v1/mp/orders/{orderId}/confirm-pay-settlement
 * @param {{ paymentChannel?: string }} [data]
 */
export function confirmPaySettlement(orderId, data = {}, options = {}) {
	const channel = data.paymentChannel || data.payment_channel || 'wechat'
	return post(
		`/orders/${orderId}/confirm-pay-settlement`,
		{ payment_channel: channel, paymentChannel: channel },
		options
	)
}

/**
 * 查询订单开票信息
 * GET /api/v1/mp/orders/{orderId}/invoice
 */
export function getOrderInvoice(orderId, options = {}) {
	return get(`/orders/${orderId}/invoice`, {}, options)
}

/**
 * 开票归档（保存开票信息并办结）
 * POST /api/v1/mp/orders/{orderId}/invoice-archive
 * @param {Object} [data] OrderCompleteRequest
 */
export function archiveOrderInvoice(orderId, data = {}, options = {}) {
	const body = {
		tax_cleared: data.taxCleared != null ? data.taxCleared : (data.tax_cleared != null ? data.tax_cleared : true),
		invoice_issued: data.invoiceIssued != null ? data.invoiceIssued : (data.invoice_issued != null ? data.invoice_issued : true),
		taxCleared: data.taxCleared != null ? data.taxCleared : (data.tax_cleared != null ? data.tax_cleared : true),
		invoiceIssued: data.invoiceIssued != null ? data.invoiceIssued : (data.invoice_issued != null ? data.invoice_issued : true)
	}
	const put = (snake, camel, val) => {
		if (val == null || val === '') return
		body[snake] = val
		body[camel] = val
	}
	put('company_name', 'companyName', data.companyName != null ? data.companyName : data.company_name)
	put('credit_code', 'creditCode', data.creditCode != null ? data.creditCode : data.credit_code)
	put('bank_account_info', 'bankAccountInfo', data.bankAccountInfo != null ? data.bankAccountInfo : data.bank_account_info)
	put('invoice_type', 'invoiceType', data.invoiceType != null ? data.invoiceType : data.invoice_type)
	put('tax_rate', 'taxRate', data.taxRate != null ? data.taxRate : data.tax_rate)
	put('invoice_amount', 'invoiceAmount', data.invoiceAmount != null ? data.invoiceAmount : data.invoice_amount)
	put('tax_amount', 'taxAmount', data.taxAmount != null ? data.taxAmount : data.tax_amount)
	put('total_with_tax', 'totalWithTax', data.totalWithTax != null ? data.totalWithTax : data.total_with_tax)
	put('invoice_no', 'invoiceNo', data.invoiceNo != null ? data.invoiceNo : data.invoice_no)
	put('tax_voucher_no', 'taxVoucherNo', data.taxVoucherNo != null ? data.taxVoucherNo : data.tax_voucher_no)
	if (data.remark) body.remark = data.remark
	return post(`/orders/${orderId}/invoice-archive`, body, options)
}

/**
 * @deprecated 请使用 archiveOrderInvoice（POST .../invoice-archive）
 * POST /api/v1/mp/orders/{orderId}/complete
 */
export function completeOrder(orderId, data = {}, options = {}) {
	return archiveOrderInvoice(orderId, data, options)
}
