/**
 * Sync Domain Adapter
 *
 * Routes sync_queue, sync_queue_dlq, and sync_queue_dlq_archive
 * to Prisma or Convex based on data-source config.
 *
 * NOTE: Convex has limited mutations/queries for this domain.
 * Most paths currently delegate to Prisma. The sync queue is
 * an infrastructure concern that will likely stay on Prisma.
 */

import { prisma } from "@/lib/db";
import { getDataSource } from "@/lib/data-source";

const DOMAIN = "sync" as const;

function isConvex(): boolean {
  return getDataSource(DOMAIN) === "convex";
}

// ─── Sync Queue ─────────────────────────────────────────────────────────────

export const syncQueueAdapter = {
  async findMany(args: {
    where?: Record<string, unknown>;
    orderBy?: Record<string, unknown>;
    skip?: number;
    take?: number;
  }) {
    return prisma.sync_queue.findMany(args as never);
  },

  async findFirst(args: { where?: Record<string, unknown> }) {
    return prisma.sync_queue.findFirst(args as never);
  },

  async create(args: { data: Record<string, unknown> }) {
    return prisma.sync_queue.create(args as never);
  },

  async update(args: {
    where: Record<string, unknown>;
    data: Record<string, unknown>;
  }) {
    return prisma.sync_queue.update(args as never);
  },

  async updateMany(args: {
    where: Record<string, unknown>;
    data: Record<string, unknown>;
  }) {
    return prisma.sync_queue.updateMany(args as never);
  },

  async delete(args: { where: Record<string, unknown> }) {
    return prisma.sync_queue.delete(args as never);
  },

  async deleteMany(args: { where: Record<string, unknown> }) {
    return prisma.sync_queue.deleteMany(args as never);
  },

  async count(args?: { where?: Record<string, unknown> }) {
    return prisma.sync_queue.count(args as never);
  },
};

// ─── Sync Queue DLQ ─────────────────────────────────────────────────────────

export const syncQueueDlqAdapter = {
  async findMany(args: {
    where?: Record<string, unknown>;
    orderBy?: Record<string, unknown>;
    skip?: number;
    take?: number;
  }) {
    return prisma.sync_queue_dlq.findMany(args as never);
  },

  async findFirst(args: { where?: Record<string, unknown> }) {
    return prisma.sync_queue_dlq.findFirst(args as never);
  },

  async create(args: { data: Record<string, unknown> }) {
    return prisma.sync_queue_dlq.create(args as never);
  },

  async update(args: {
    where: Record<string, unknown>;
    data: Record<string, unknown>;
  }) {
    return prisma.sync_queue_dlq.update(args as never);
  },

  async delete(args: { where: Record<string, unknown> }) {
    return prisma.sync_queue_dlq.delete(args as never);
  },

  async deleteMany(args: { where: Record<string, unknown> }) {
    return prisma.sync_queue_dlq.deleteMany(args as never);
  },

  async count(args?: { where?: Record<string, unknown> }) {
    return prisma.sync_queue_dlq.count(args as never);
  },
};

// ─── Sync Queue DLQ Archive ─────────────────────────────────────────────────

export const syncQueueDlqArchiveAdapter = {
  async findMany(args: {
    where?: Record<string, unknown>;
    orderBy?: Record<string, unknown>;
    skip?: number;
    take?: number;
  }) {
    return prisma.sync_queue_dlq_archive.findMany(args as never);
  },

  async create(args: { data: Record<string, unknown> }) {
    return prisma.sync_queue_dlq_archive.create(args as never);
  },

  async count(args?: { where?: Record<string, unknown> }) {
    return prisma.sync_queue_dlq_archive.count(args as never);
  },
};

// Suppress unused isConvex warning — will be used when Convex paths are added
void isConvex;
