import { json } from '@sveltejs/kit';
import { error } from '@sveltejs/kit';
import type { RequestHandler } from './$types';

import {
  TOKEN
} from '$env/static/private'

import PG from '$lib/server/database';
import { getConnection } from '$lib/server/database/get_connection';
// import { building } from '$app/environment';
import init from '$lib/server/database/init_db';

export const POST: RequestHandler = async ({ url }) => {
  const token = url.searchParams.get('token');
  console.log(token);
  if (token !== TOKEN) {
    return error(401, 'Unauthorized');
  }

  await init(await PG(getConnection()));
  return new Response(String(token));
};
