import express from "express";
import request from "supertest";
import { describe, expect, it, vi } from "vitest";

vi.mock("../db/db.controller.js", () => ({
	db: {},
}));

vi.mock("../logger/logger.controller.js", () => ({
	createLogger: () => ({
		info: vi.fn(),
		error: vi.fn(),
		warn: vi.fn(),
		debug: vi.fn(),
	}),
}));

vi.mock("./datamart.controller.js", () => ({
	runDatamartQuery: vi.fn(async ({ name, options }) => ({
		success: true,
		message: `Ran ${name}`,
		data: {
			name,
			options,
		},
	})),
}));

import { runDatamartQuery } from "./datamart.controller.js";
import getDatamartRoute from "./getDatamart.route.js";

function createTestApp() {
	const app = express();

	app.use(express.json());
	app.use("/datamart", getDatamartRoute);

	return app;
}

describe("GET /datamart/:name", () => {
	it("runs a datamart query by name and returns api response", async () => {
		const app = createTestApp();

		const res = await request(app).get("/datamart/orders").query({
			value: "123",
		});

		expect(res.status).toBe(200);

		expect(runDatamartQuery).toHaveBeenCalledWith({
			name: "orders",
			options: {
				value: "123",
			},
		});

		expect(res.body.success).toBe(true);
		expect(res.body.module).toBe("datamart");
		expect(res.body.subModule).toBe("query");
		expect(res.body.data).toEqual({
			name: "orders",
			options: {
				value: "123",
			},
		});
	});
});
