import type { Kysely } from "kysely";
import type { DB } from "kysely-codegen";
import type {
  AdminLogAction,
  AdminLogEntity,
  ChatbotAdminLogTable,
} from "@/internal/datastore/chatbot.types";

type DBWithAdminLog = DB & {
  chatbot_admin_log: ChatbotAdminLogTable;
};

export async function insertAdminLog(
  db: Kysely<DB> | Kysely<DBWithAdminLog>,
  params: {
    entity_type: AdminLogEntity;
    entity_id: string;
    action: AdminLogAction;
    performed_by: string;
    before_data: any | null;
    after_data: any | null;
  },
) {
  const {
    entity_type,
    entity_id,
    action,
    performed_by,
    before_data,
    after_data,
  } = params;

  await (db as Kysely<DBWithAdminLog>)
    .insertInto("chatbot_admin_log")
    .values({
      entity_type,
      entity_id,
      action,
      performed_by,
      before_data: before_data ?? null,
      after_data: after_data ?? null,
    })
    .execute();
}
