import { beforeEach, describe, expect, it } from "bun:test";
import { Hono } from "hono";
import { categoryRoutes } from "../../src/server/routes/categories";
import { itemRoutes } from "../../src/server/routes/items";
import { setupRoutes } from "../../src/server/routes/setups";
import { threadRoutes } from "../../src/server/routes/threads";
import { createTestDb } from "../helpers/db";

async function createTestApp() {
	const { db, userId } = await createTestDb();
	const app = new Hono();
	app.use("*", async (c, next) => {
		c.set("db", db);
		c.set("userId", userId);
		await next();
	});
	app.route("/api/items", itemRoutes);
	app.route("/api/categories", categoryRoutes);
	app.route("/api/threads", threadRoutes);
	app.route("/api/setups", setupRoutes);
	return app;
}

describe("Invalid ID parameter handling", () => {
	let app: Hono;

	beforeEach(async () => {
		app = await createTestApp();
	});

	describe("items", () => {
		it("GET /api/items/abc returns 400", async () => {
			const res = await app.request("/api/items/abc");
			expect(res.status).toBe(400);
			const body = await res.json();
			expect(body.error).toContain("Invalid");
		});

		it("GET /api/items/0 returns 400", async () => {
			const res = await app.request("/api/items/0");
			expect(res.status).toBe(400);
		});

		it("GET /api/items/-1 returns 400", async () => {
			const res = await app.request("/api/items/-1");
			expect(res.status).toBe(400);
		});
	});

	describe("categories", () => {
		it("DELETE /api/categories/abc returns 400", async () => {
			const res = await app.request("/api/categories/abc", {
				method: "DELETE",
			});
			expect(res.status).toBe(400);
		});
	});

	describe("threads", () => {
		it("GET /api/threads/abc returns 400", async () => {
			const res = await app.request("/api/threads/abc");
			expect(res.status).toBe(400);
		});

		it("GET /api/threads/1.5 returns 400", async () => {
			const res = await app.request("/api/threads/1.5");
			expect(res.status).toBe(400);
		});
	});

	describe("setups", () => {
		it("GET /api/setups/abc returns 400", async () => {
			const res = await app.request("/api/setups/abc");
			expect(res.status).toBe(400);
		});

		it("GET /api/setups/0 returns 400", async () => {
			const res = await app.request("/api/setups/0");
			expect(res.status).toBe(400);
		});
	});
});
