import type { TailorAnyDBType } from "@tailor-platform/sdk";
import { registerAuditableEntity } from "./command/registerAuditableEntity.generated";
import { createAuditPolicy } from "./command/createAuditPolicy.generated";
import { updateAuditPolicy } from "./command/updateAuditPolicy.generated";
import { activateAuditPolicy } from "./command/activateAuditPolicy.generated";
import { deactivateAuditPolicy } from "./command/deactivateAuditPolicy.generated";
import { reactivateAuditPolicy } from "./command/reactivateAuditPolicy.generated";
import { replaceAuditPolicy } from "./command/replaceAuditPolicy.generated";
import { deleteAuditPolicy } from "./command/deleteAuditPolicy.generated";
import { logAuditEvent } from "./command/logAuditEvent.generated";
import { getAuditEntry } from "./query/getAuditEntry.generated";
import { getAuditPolicy } from "./query/getAuditPolicy.generated";
import { getAuditSummary } from "./query/getAuditSummary.generated";
import { getChangeDetails } from "./query/getChangeDetails.generated";
import { listAuditPolicies } from "./query/listAuditPolicies.generated";
import { searchAuditEntries } from "./query/searchAuditEntries.generated";
import { auditableEntity } from "./db/auditableEntity";
import { createAuditPolicyType } from "./db/auditPolicy";
import { policyFieldRule } from "./db/policyFieldRule";
import { createAuditEntryType } from "./db/auditEntry";
import { changeDetail } from "./db/changeDetail";
import { company as companyStub } from "./lib/_db_deps";

export { companyStub };

export interface DefineModuleParams {
  organization?: {
    db: { company: TailorAnyDBType };
  };
}

/* @__NO_SIDE_EFFECTS__ */
export const defineModule = (params?: DefineModuleParams) => {
  const companyType = params?.organization?.db.company;

  const auditPolicy = createAuditPolicyType({ companyType });
  const auditEntry = createAuditEntryType({ companyType });

  return {
    db: { auditableEntity, auditPolicy, policyFieldRule, auditEntry, changeDetail },
    commands: {
      registerAuditableEntity: registerAuditableEntity(),
      createAuditPolicy: createAuditPolicy(),
      updateAuditPolicy: updateAuditPolicy(),
      activateAuditPolicy: activateAuditPolicy(),
      deactivateAuditPolicy: deactivateAuditPolicy(),
      reactivateAuditPolicy: reactivateAuditPolicy(),
      replaceAuditPolicy: replaceAuditPolicy(),
      deleteAuditPolicy: deleteAuditPolicy(),
      logAuditEvent: logAuditEvent(),
    },
    queries: {
      getAuditEntry,
      getAuditPolicy,
      getAuditSummary,
      getChangeDetails,
      listAuditPolicies,
      searchAuditEntries,
    },
  };
};
