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

export const RemoveMemberInput = z.object({ companyId: z.string(), memberId: z.string() })
export const RemoveMemberOutput = z.object({ ok: z.literal(true) })

export const removeMember = pikkuFunc({
  expose: true,
  description: 'Remove a member from a company. Caller must be managing_director.',
  input: RemoveMemberInput,
  output: RemoveMemberOutput,
  permissions: { md: [isManagingDirector] },
  func: async ({ kysely, audit }, { companyId, memberId }) => {
    await kysely
      .deleteFrom('companyMember')
      .where('memberId', '=', memberId)
      .where('companyId', '=', companyId)
      .execute()
    await audit.onDelete('companyMember', memberId)
    return { ok: true as const }
  },
})
