import { z } from 'zod'
import { pikkuFunc } from '#pikku/pikku-types.gen.js'
import { isManagingDirector } from '../../lib/auth-helpers.js'

export const ArchiveResolutionInput = z.object({ resolutionId: z.string() })
export const ArchiveResolutionOutput = z.object({ ok: z.literal(true) })

export const archiveResolution = pikkuFunc({
  expose: true,
  description: 'Archive a resolution. Caller must be managing_director.',
  input: ArchiveResolutionInput,
  output: ArchiveResolutionOutput,
  permissions: { md: [isManagingDirector] },
  func: async ({ kysely, audit }, { resolutionId }) => {
    await kysely
      .updateTable('resolution')
      .set({ status: 'archived' })
      .where('resolutionId', '=', resolutionId)
      .execute()
    await audit.onUpdate('resolution', resolutionId, { status: 'archived' })
    return { ok: true as const }
  },
})
