import { integer, pgTable, timestamp } from "drizzle-orm/pg-core";

export const prodAuditLogState = pgTable("prod_audit_log_state", {
	id: integer("id").primaryKey().default(1),

	lastImportedAuditId: integer("last_imported_audit_id").notNull().default(0),
	lastProcessedAuditId: integer("last_processed_audit_id").notNull().default(0),

	createdAt: timestamp("created_at", { withTimezone: true }).defaultNow(),
	updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow(),
});

/*
if the system fails do the process we do 
and increase the retry to x max of 5 tries
const nextRetryAt = new Date(Date.now() + Math.min(30 * retryCount, 600) * 1000);

Cron every 30s
  ↓
Pull ERP AuditLog by Id > lastAuditId
  ↓
Insert into prod_audit_log
  ↓
Postgres NOTIFY wakes worker
  ↓
Worker processes pending rows
  ↓
Success = success
Failure = error + retryCount + nextRetryAt
20 failures = dead + email


for the check we want to do 

status IN ('pending', 'error')
AND retry_count < 20
AND (next_retry_at IS NULL OR next_retry_at <= NOW())

*/
