import { serve } from "bun";
import index from "./index.html";
import wisher from "./wisher/index.html";
import GarageApp from "./GarageApp/index.html";
import locales from "./GarageApp/locales/index";
import { Database } from "bun:sqlite";

const db = new Database("src/GarageApp/hammond.db", {create: true});

//	console.log(`${lng}, ${ns}`);
//	console.log(process.cwd());
const server = serve({
  routes: {
    // Serve index.html for all unmatched routes.
    "/*": index,

    "/wisher": wisher,
    "/wisher/*": wisher,

    "/GarageApp": GarageApp,
    "/GarageApp/*": GarageApp,
    "/GarageApp/locales/:lng/translation.json": async req => {
	const lng = req.params.lng;
	const ns = req.params.ns;
	const path = `src/GarageApp/locales/${lng}/translation.json`
	return new Response(Bun.file(path))
    },
    "/GarageApp/api/vehicles": async req => {
	    const uid = "39e9009e-50e1-4277-bbf7-69e5e0f6c6dc"
	    const query = db.query(
		    `SELECT * FROM vehicles WHERE id IN (SELECT vehicle_id FROM user_vehicles WHERE user_id='${uid}');`
	    );
//	    console.log(query.toString())
	    const vehicles = query.all();
//	    console.log(vehicles);
	    return Response.json(vehicles)
    },



    "/api/hello": {
      async GET(req) {
        return Response.json({
          message: "Hello, world!",
          method: "GET",
        });
      },
      async PUT(req) {
        return Response.json({
          message: "Hello, world!",
          method: "PUT",
        });
      },
    },

    "/api/hello/:name": async req => {
      const name = req.params.name;
      return Response.json({
        message: `Hello, ${name}!`,
      });
    },
  },

  development: process.env.NODE_ENV !== "production" && {
    // Enable browser hot reloading in development
    hmr: true,

    // Echo console logs from the browser to the server
    console: true,
  },
});

console.log(`🚀 Server running at ${server.url}`);
