import invariant from "tiny-invariant";
import { toHex } from "viem";
import { ADMIN_CREATE_ORDER_IN_MATCHING_POOL_ROUTE } from "../constants.js";
import type { Config } from "../createConfig.js";
import { stringifyForWasm } from "../utils/bigJSON.js";
import { postRelayerWithAdmin } from "../utils/http.js";
import type { CreateOrderParameters, CreateOrderReturnType } from "./createOrder.js";
import { getBackOfQueueWallet } from "./getBackOfQueueWallet.js";
import { getWalletId } from "./getWalletId.js";

export type CreateOrderInMatchingPoolParameters = {
    matchingPool: string;
} & CreateOrderParameters;

export async function createOrderInMatchingPool(
    config: Config,
    parameters: CreateOrderInMatchingPoolParameters,
): Promise<CreateOrderReturnType> {
    const logger = config.getLogger("core:actions:createOrderInMatchingPool");
    const {
        id = "",
        base,
        quote,
        side,
        amount,
        worstCasePrice = "",
        minFillSize = BigInt(0),
        allowExternalMatches = false,
        matchingPool,
    } = parameters;
    const {
        getBaseUrl,
        utils,
        state: { seed },
    } = config;
    invariant(seed, "Seed is required");

    const walletId = getWalletId(config);
    const wallet = await getBackOfQueueWallet(config);

    const body = await utils.new_order_in_matching_pool(
        seed,
        stringifyForWasm(wallet),
        id,
        base,
        quote,
        side,
        toHex(amount),
        worstCasePrice,
        toHex(minFillSize),
        allowExternalMatches,
        matchingPool,
    );

    try {
        const res = await postRelayerWithAdmin(
            config,
            getBaseUrl(ADMIN_CREATE_ORDER_IN_MATCHING_POOL_ROUTE(walletId)),
            body,
        );
        logger.debug(`task update-wallet(${res.task_id})`, {
            walletId,
            orderId: id,
            taskId: res.task_id,
            matchingPool,
        });
        return { taskId: res.task_id };
    } catch (error) {
        logger.error(
            `Create order in matching pool failed: ${error instanceof Error ? error.message : String(error)}`,
            { walletId, orderId: id },
        );
        throw error;
    }
}
