import { db, type TailorAnyDBType } from "@tailor-platform/sdk";
import { defaultGqlPermission, defaultPermission } from "../../../shared/modelPermissions";

export interface CreateAccountingEventTypeParams {
  companyType?: TailorAnyDBType;
}

export function createAccountingEventType(params: CreateAccountingEventTypeParams = {}) {
  const companyId = db.uuid().description("Foreign key to Company");
  return db
    .type("AccountingEvent", {
      companyId: params.companyType
        ? companyId.relation({
            type: "n-1",
            toward: { type: params.companyType },
            backward: "accountingEvents",
          })
        : companyId,
      eventKind: db.string().description("Business accounting event kind"),
      schemaVersion: db.string().description("Payload schema version"),
      occurredAt: db.datetime().description("Timestamp when the accounting event occurred"),
      payloadJson: db.string().description("Resolver-neutral immutable payload snapshot as JSON"),
      ...db.fields.timestamps(),
    })
    .permission(defaultPermission)
    .gqlPermission(defaultGqlPermission);
}

export const accountingEvent = createAccountingEventType({});
