import { zValidator } from "@hono/zod-validator";
import { Hono } from "hono";
import { createItemSchema, updateItemSchema } from "../../shared/schemas.ts";
import { parseId } from "../lib/params.ts";
import { exportItemsCsv, importItemsCsv } from "../services/csv.service.ts";
import {
	createItem,
	deleteItem,
	duplicateItem,
	getAllItems,
	getItemById,
	updateItem,
} from "../services/item.service.ts";
import {
	deleteImage,
	withImageUrl,
	withImageUrls,
} from "../services/storage.service.ts";

type Env = { Variables: { db?: any; userId?: number } };

const app = new Hono<Env>();

app.get("/export", async (c) => {
	const db = c.get("db");
	const userId = c.get("userId")!;
	const csv = await exportItemsCsv(db, userId);
	c.header("Content-Type", "text/csv");
	c.header("Content-Disposition", 'attachment; filename="gearbox-export.csv"');
	return c.body(csv);
});

app.post("/import", async (c) => {
	const db = c.get("db");
	const userId = c.get("userId")!;
	const body = await c.req.parseBody();
	// Accept either "file" (direct) or "image" (via apiUpload helper)
	const file = body.file ?? body.image;
	if (!file || typeof file === "string") {
		return c.json({ error: "No CSV file provided" }, 400);
	}
	const content = await file.text();
	const result = await importItemsCsv(db, userId, content);
	return c.json(result);
});

app.get("/", async (c) => {
	const db = c.get("db");
	const userId = c.get("userId")!;
	const items = await getAllItems(db, userId);
	return c.json(await withImageUrls(items));
});

app.get("/:id", async (c) => {
	const db = c.get("db");
	const userId = c.get("userId")!;
	const id = parseId(c.req.param("id"));
	if (!id) return c.json({ error: "Invalid item ID" }, 400);
	const item = await getItemById(db, userId, id);
	if (!item) return c.json({ error: "Item not found" }, 404);
	return c.json(await withImageUrl(item));
});

app.post("/", zValidator("json", createItemSchema), async (c) => {
	const db = c.get("db");
	const userId = c.get("userId")!;
	const data = c.req.valid("json");
	const item = await createItem(db, userId, data);
	return c.json(item, 201);
});

app.put(
	"/:id",
	zValidator("json", updateItemSchema.omit({ id: true })),
	async (c) => {
		const db = c.get("db");
		const userId = c.get("userId")!;
		const id = parseId(c.req.param("id"));
		if (!id) return c.json({ error: "Invalid item ID" }, 400);
		const data = c.req.valid("json");
		const item = await updateItem(db, userId, id, data);
		if (!item) return c.json({ error: "Item not found" }, 404);
		return c.json(item);
	},
);

app.post("/:id/duplicate", async (c) => {
	const db = c.get("db");
	const userId = c.get("userId")!;
	const id = parseId(c.req.param("id"));
	if (!id) return c.json({ error: "Invalid item ID" }, 400);
	const newItem = await duplicateItem(db, userId, id);
	if (!newItem) return c.json({ error: "Item not found" }, 404);
	return c.json(newItem, 201);
});

app.delete("/:id", async (c) => {
	const db = c.get("db");
	const userId = c.get("userId")!;
	const id = parseId(c.req.param("id"));
	if (!id) return c.json({ error: "Invalid item ID" }, 400);
	const deleted = await deleteItem(db, userId, id);
	if (!deleted) return c.json({ error: "Item not found" }, 404);

	// Clean up image from object storage if exists
	if (deleted.imageFilename) {
		try {
			await deleteImage(deleted.imageFilename);
		} catch {
			// Missing object is not an error worth failing the delete over
		}
	}

	return c.json({ success: true });
});

export { app as itemRoutes };
