import type ImportBatch from '#models/import_batch'
import type ImportDraft from '#models/import_draft'
import { BaseTransformer } from '@adonisjs/core/transformers'

function serializeDraft(d: ImportDraft) {
  return {
    id: d.id,
    filename: d.filename,
    pdfStorageKey: d.pdfStorageKey,
    extracted: d.extracted,
    edited: d.edited,
    confidence: d.confidence,
    status: d.status,
    invoiceId: d.invoiceId,
    createdAt: d.createdAt.toISO()!,
    updatedAt: d.updatedAt?.toISO() ?? d.createdAt.toISO()!,
  }
}

export default class ImportBatchTransformer extends BaseTransformer<ImportBatch> {
  toObject() {
    const b = this.resource
    return {
      id: b.id,
      organizationId: b.organizationId,
      drafts: (b.drafts ?? []).map(serializeDraft),
      createdAt: b.createdAt.toISO()!,
      updatedAt: b.updatedAt?.toISO() ?? b.createdAt.toISO()!,
    }
  }
}

export { serializeDraft }
