import { defineAction } from 'astro:actions';
import { z } from 'astro:schema';
import { Session } from '@util/session.ts';
import { Permissions } from '@util/permissions.ts';
import { db } from '@db/database.ts';

export const settings = {
  setSettings: defineAction({
    input: z.object({
      settings: z.array(
        z.object({
          name: z.string(),
          value: z.string().nullable()
        })
      )
    }),
    handler: async (input, context) => {
      Session.actionSessionFromCookies(context.cookies, Permissions.Settings);

      await db.setSettings(input);
    }
  })
};
