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/postgres';
import { building } from '$app/environment';
import { reload as reload_data } from '$lib/server/data_loader';

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

  if (!building) await reload_data(await PG(locals.db), locals.git);
  return new Response(String(token));
};
