import type { SanitizedConfig } from "payload";
import type { AuditLogProtocol } from "@repo/core-shared/di";
import type { IDataExport } from "../data-export.interface";
import type { IDataDelete } from "../data-delete.interface";
import type { IDataRectify } from "../data-rectify.interface";
import type { IProcessingRestriction } from "../processing-restriction.interface";
import { PayloadDataExport } from "../payload-data-export";
import { PayloadDataDelete } from "../payload-data-delete";
import { PayloadDataRectify } from "../payload-data-rectify";
import { PayloadProcessingRestriction } from "../payload-processing-restriction";

export type DsrBinding = {
  dataExport: IDataExport;
  dataDelete: IDataDelete;
  dataRectify: IDataRectify;
  processingRestriction: IProcessingRestriction;
};

export type BindProductionDsrOpts = {
  config: SanitizedConfig;
  auditLog?: AuditLogProtocol;
};

const noopAuditLog: AuditLogProtocol = { record: async () => {} };

/**
 * Returns Payload-backed DSR implementations pre-wired with config + auditLog.
 *
 * Wired by the app aggregator alongside feature binders. The returned binding
 * is passed as a dependency to dsrRouter and any feature that needs DSR
 * operations.
 */
export function bindProductionDsr(opts: BindProductionDsrOpts): DsrBinding {
  const auditLog = opts.auditLog ?? noopAuditLog;
  return {
    dataExport: new PayloadDataExport(opts.config, auditLog),
    dataDelete: new PayloadDataDelete(opts.config, auditLog),
    dataRectify: new PayloadDataRectify(opts.config, auditLog),
    processingRestriction: new PayloadProcessingRestriction(
      opts.config,
      auditLog,
    ),
  };
}
