import { get, post, postForm } from '../utils/request';
import { resolveLoginPlatform } from './auth';

export interface MerchantConfigQuery {
	merchant_id?: string;
	scene?: string;
}

export interface CashierOrderPayload {
	merchant_id?: string;
	amount: string;
	remark?: string;
	house_id?: string;
}

export interface CashierOrderResult {
	order_id: string;
}

export interface CashierPayResult {
	timeStamp: string;
	nonceStr: string;
	package: string;
	signType: string;
	paySign: string;
}

export interface BoundMerchantInfo {
	merchant_id?: string;
	merchant_name?: string;
	config?: {
		score_name?: string;
	};
	merchant?: {
		merchant_id?: string;
		merchant_name?: string;
		config?: {
			score_name?: string;
		};
	};
	supplier?: {
		fee_rate?: string | number;
	};
	fee_rate?: string | number;
}

export interface SupplierInfo {
	supplier_id?: string | number;
	supplier_name?: string;
	logo?: string;
	fee_rate?: string | number;
	supplier?: {
		supplier_id?: string | number;
		supplier_name?: string;
		logo?: string;
		fee_rate?: string | number;
	};
}

export interface MerchantListItem {
	merchant_id: string | number;
	merchant_name: string;
}

export interface ApiResult<T = any> {
	code: number | string;
	msg?: string;
	data?: T;
}

export interface TradeQueryResult {
	order_id?: number | string;
	trade_no?: string;
	pay_status?: number;
	pay_type?: number;
	total_money?: string;
	reduce_money?: string;
	real_money?: string;
}

export interface TradeListItem {
	trade_no: string;
	pay_status: number;
	real_money: string;
	merchant_id?: number | string;
	create_time?: string;
	wuye_money?: string;
	pay_time?: string | null;
	refund_time?: string | null;
	pay_type?: number;
	supplier_name?: string;
}

export interface TradeListQuery {
	trade_no?: string;
	page?: number | string;
	limit?: number | string;
}

export interface PlatformScoreResult {
	score?: string | number;
}

export interface ScoreRecordItem {
	id?: string | number;
	score?: string | number;
	change_score?: string | number;
	type?: string | number;
	remark?: string;
	create_time?: string;
	[key: string]: unknown;
}

export interface ScoreRecordQuery {
	page?: number | string;
	limit?: number | string;
}

export interface CashTradeInfo {
	trade_no?: string;
	pay_status?: number;
	pay_type?: number;
	total_money?: string;
	reduce_money?: string;
	real_money?: string;
	merchant_id?: number | string;
	supplier_id?: number | string;
	timeout?: number;
	create_time?: string;
}

export interface CashTradeInfoResult {
	trade?: CashTradeInfo;
	body?: string;
	subject?: string;
}

export interface CashTradePayPayload {
	trade_no: string;
	platform?: string;
	remark?: string;
}

export interface CashTradePayResult {
	trade_no?: string;
	app_id?: string;
	timestamp?: string;
	nonce_str?: string;
	package?: string;
	sign_type?: string;
	pay_sign?: string;
}

export const merchantConfig = (data: MerchantConfigQuery = {}) => get<ApiResult>('v1/cashier/merchant', data);

export const createCashierOrder = (data: CashierOrderPayload) =>
	post<ApiResult<CashierOrderResult>>('v1/cashier/order/create', data);

export const cashierPay = (data: { order_id: string }) => post<ApiResult<CashierPayResult>>('v1/cashier/pay', data);

export const cashierRecordList = (data: any = {}) => get('v1/cashier/records', data);

export const tradeList = (data: TradeListQuery = {}) =>
	get<ApiResult<TradeListItem[]>>('user/v1/trade.list', {
		trade_no: data.trade_no != null ? data.trade_no : '',
		page: data.page != null ? data.page : 1,
		limit: data.limit != null ? data.limit : 10
	});

export const bindMerchantByUrl = (url: string) =>
	get<ApiResult<BoundMerchantInfo>>('user/v1/bing.merchant', { url });

export const supplierByUrl = (url: string) =>
	get<ApiResult<SupplierInfo>>('user/v1/supplier', { url });

export const merchantListByUrl = (url: string) =>
	get<ApiResult<MerchantListItem[]>>('user/v1/merchant.list', { url });

export const changeMerchant = (data: { merchant_id: string | number; url: string }) =>
	postForm<ApiResult>('user/v1/merchant.change', data);

export const queryTrade = (tradeNo: string) =>
	get<ApiResult<TradeQueryResult>>('user/v1/trade/query', { trade_no: tradeNo });

export const cancelTrade = (tradeNo: string) =>
	get<ApiResult>('user/v1/trade/cancel', { trade_no: tradeNo });

export const platformScore = () => get<ApiResult<PlatformScoreResult>>('user/v1/platform.score');

export const platformScoreRecord = (data: ScoreRecordQuery = {}) =>
	get<ApiResult<ScoreRecordItem[]>>('user/v1/platform.score.record', {
		page: data.page != null ? data.page : 1,
		limit: data.limit != null ? data.limit : 10
	});

export const cashTradeInfo = (tradeNo: string) =>
	get<ApiResult<CashTradeInfoResult>>('user/v1/cash.trade.info', { trade_no: tradeNo });

export const cashTradePay = (data: CashTradePayPayload) =>
	post<ApiResult<CashTradePayResult>>('user/v1/cash.trade', {
		trade_no: data.trade_no,
		platform: data.platform != null ? data.platform : resolveLoginPlatform(),
		remark: data.remark != null ? data.remark : ''
	});
