import { beforeEach, describe, expect, it } from "bun:test";
import { Hono } from "hono";
import { tags } from "../../src/db/schema.ts";
import { tagRoutes } from "../../src/server/routes/tags.ts";
import { createTestDb } from "../helpers/db.ts";

function createTestApp(db: any) {
	const app = new Hono();

	app.use("*", async (c, next) => {
		c.set("db", db);
		await next();
	});

	app.route("/api/tags", tagRoutes);
	return app;
}

describe("Tag Routes", () => {
	let app: Hono;
	let db: Awaited<ReturnType<typeof createTestDb>>["db"];

	beforeEach(async () => {
		const testDb = await createTestDb();
		db = testDb.db;
		app = createTestApp(db);
	});

	describe("GET /api/tags", () => {
		it("returns 200 with empty array when no tags", async () => {
			const res = await app.request("/api/tags");
			expect(res.status).toBe(200);

			const body = await res.json();
			expect(body).toEqual([]);
		});

		it("returns 200 with tag objects after seeding", async () => {
			await db
				.insert(tags)
				.values([{ name: "bikepacking" }, { name: "ultralight" }]);

			const res = await app.request("/api/tags");
			expect(res.status).toBe(200);

			const body = await res.json();
			expect(body).toHaveLength(2);
			expect(body[0]).toHaveProperty("id");
			expect(body[0]).toHaveProperty("name");
		});
	});
});
