export interface PayCheckoutOrder {
	orderId: string;
	tradeNo: string;
	amount: string;
	merchantName: string;
	orderDesc: string;
	remark: string;
	expireAt: number;
	payChannel: 'wxpay';
}

// Static mock for embedded checkout page; replace with API later.
export const MOCK_PAY_CHECKOUT_ORDER: PayCheckoutOrder = {
	orderId: 'mock-order-20260702001',
	tradeNo: 'GSP202607021430001234',
	amount: '1.25',
	merchantName: '江南米粉',
	orderDesc: '消费者付款',
	remark: '',
	expireAt: Date.now() + 5 * 60 * 1000,
	payChannel: 'wxpay'
};

export const buildMockOrderFromOptions = (options: Record<string, string> = {}): PayCheckoutOrder => {
	const amount = options.amount || MOCK_PAY_CHECKOUT_ORDER.amount;
	const expireSeconds = Number(options.expire_seconds || 300);

	return {
		orderId: options.order_id || options.orderId || MOCK_PAY_CHECKOUT_ORDER.orderId,
		tradeNo: options.trade_no || options.tradeNo || MOCK_PAY_CHECKOUT_ORDER.tradeNo,
		amount,
		merchantName: decodeQuery(options.merchant_name || options.merchantName) || MOCK_PAY_CHECKOUT_ORDER.merchantName,
		orderDesc: decodeQuery(options.order_desc || options.orderDesc) || MOCK_PAY_CHECKOUT_ORDER.orderDesc,
		remark: decodeQuery(options.remark) || '',
		expireAt: Date.now() + (Number.isFinite(expireSeconds) ? expireSeconds : 300) * 1000,
		payChannel: 'wxpay'
	};
};

const decodeQuery = (value?: string) => {
	if (!value) return '';
	try {
		return decodeURIComponent(value);
	} catch (error) {
		return value;
	}
};
