import { pikkuFunc } from '#pikku/pikku-types.gen.js'
import type {
  DB,
  RecognitionDossierDetail,
  RecognitionDossierSummary,
} from '@heygermany/sdk'
import { BadRequestError, NotFoundError } from '@pikku/core/errors'
import { isBackOffice } from '../../permissions.js'

export const getBackOfficeRecognitionDossiers = pikkuFunc<
  { candidateId: string },
  RecognitionDossierSummary[]
>({
  expose: true,
  permissions: { isBackOffice },
  title: 'Get recognition dossiers',
  description: 'Fetches all recognition dossiers for a candidate',
  errors: [NotFoundError],
  func: async ({ recognitionDossierService }, { candidateId }) =>
    await recognitionDossierService.listCandidateDossiers(candidateId),
})

export const getBackOfficeRecognitionDossier = pikkuFunc<
  { candidateId: string; dossierId: string },
  RecognitionDossierDetail
>({
  expose: true,
  permissions: { isBackOffice },
  title: 'Get recognition dossier',
  description: 'Fetches one recognition dossier with checklist, documents, and download state',
  errors: [NotFoundError],
  func: async ({ recognitionDossierService }, { candidateId, dossierId }) =>
    await recognitionDossierService.getCandidateDossier(candidateId, dossierId),
})

export const createBackOfficeRecognitionDossier = pikkuFunc<
  {
    candidateId: string
    targetState: DB.GermanState
    targetProfession: DB.NurseRecognitionGermany
  },
  RecognitionDossierDetail
>({
  expose: true,
  permissions: { isBackOffice },
  title: 'Create recognition dossier',
  description: 'Creates or refreshes the state-specific recognition dossier for a candidate',
  errors: [NotFoundError],
  func: async (
    { recognitionDossierService },
    { candidateId, targetState, targetProfession }
  ) =>
    await recognitionDossierService.createOrRefreshDossier({
      candidateId,
      targetState,
      targetProfession,
    }),
})

export const requestBackOfficeRecognitionDossierDocumentUploads = pikkuFunc<
  {
    candidateId: string
    dossierId: string
    documents: Array<{
      contentType: string
      size: number
      category: DB.RecognitionDossierDocumentCategory
      fileName: string
    }>
  },
  Array<{
    documentId: string
    uploadUrl: string
    assetKey: string
    uploadHeaders?: Record<string, string>
    uploadMethod?: 'PUT' | 'POST'
  }>
>({
  expose: true,
  permissions: { isBackOffice },
  title: 'Request recognition dossier document uploads',
  description: 'Creates dossier document records and returns signed upload URLs',
  errors: [BadRequestError, NotFoundError],
  func: async ({ recognitionDossierService }, input) =>
    await recognitionDossierService.requestDocumentUploads(input),
})

export const completeBackOfficeRecognitionDossierDocumentUpload = pikkuFunc<
  {
    candidateId: string
    dossierId: string
    documentId: string
    uploaded?: true
  },
  RecognitionDossierDetail
>({
  expose: true,
  permissions: { isBackOffice },
  title: 'Complete recognition dossier document upload',
  description: 'Marks a dossier document as uploaded and refreshes dossier readiness',
  errors: [NotFoundError],
  func: async ({ recognitionDossierService }, { candidateId, dossierId, documentId, uploaded }) =>
    await recognitionDossierService.completeDocumentUpload({
      candidateId,
      dossierId,
      documentId,
      uploaded: uploaded === true,
    }),
})

export const deleteBackOfficeRecognitionDossierDocument = pikkuFunc<
  {
    candidateId: string
    dossierId: string
    documentId: string
  },
  RecognitionDossierDetail
>({
  expose: true,
  permissions: { isBackOffice },
  title: 'Delete recognition dossier document',
  description: 'Deletes a dossier-only document and refreshes dossier readiness',
  errors: [NotFoundError],
  func: async ({ recognitionDossierService }, input) =>
    await recognitionDossierService.deleteDocument(input),
})

export const generateBackOfficeRecognitionDossierPackage = pikkuFunc<
  { candidateId: string; dossierId: string },
  RecognitionDossierDetail
>({
  expose: true,
  permissions: { isBackOffice },
  title: 'Generate recognition dossier package',
  description: 'Builds and stores the recognition dossier package when the dossier is ready',
  errors: [BadRequestError, NotFoundError],
  func: async ({ recognitionDossierService }, input) =>
    await recognitionDossierService.generatePackage(input),
})

export const getBackOfficeRecognitionDossierDownload = pikkuFunc<
  { candidateId: string; dossierId: string },
  { url: string }
>({
  expose: true,
  permissions: { isBackOffice },
  title: 'Get recognition dossier download URL',
  description: 'Fetches a signed download URL for the generated dossier package',
  errors: [BadRequestError, NotFoundError],
  func: async ({ recognitionDossierService }, input) => ({
    url: await recognitionDossierService.getDownloadUrl(input),
  }),
})
