import { createRouter } from '../../create-router';
import { getDockPaths, setDockPaths } from 'officerdb';
import * as errors from '@@/custom-errors';

export const dockRouter = createRouter();

// GET / — return dock paths, or null if none saved (frontend uses defaults)
dockRouter.get('/', async (ctx) => {
  const userId = ctx.get('user').id;
  const paths = await getDockPaths(userId);
  return ctx.json(paths);
});

// PUT / — full replacement of dock paths array
dockRouter.put('/', async (ctx) => {
  const userId = ctx.get('user').id;
  const body = ctx.get('body') as unknown;
  if (!Array.isArray(body) || body.some((p) => typeof p !== 'string')) {
    throw errors.BAD_REQUEST('Expected an array of dock paths');
  }
  const paths = body as string[];
  await setDockPaths(userId, paths);
  return ctx.json(paths);
});
