/**
 * Misc Domain Adapter
 *
 * Routes company_profiles, system_settings, and other miscellaneous
 * models to Prisma or Convex based on data-source config.
 *
 * NOTE: Convex has no mutations/queries yet for this domain —
 * all paths currently delegate to Prisma. The Convex path is
 * scaffolded for when mutations are added.
 */

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

const DOMAIN = "sync" as const; // misc uses 'sync' domain key in data-source.ts

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

// ─── Company Profiles ───────────────────────────────────────────────────────

export const companyProfilesAdapter = {
  async findMany(args: {
    where?: Record<string, unknown>;
    orderBy?: Record<string, unknown>;
    skip?: number;
    take?: number;
  }) {
    // Always Prisma — no Convex mutations/queries for this model yet
    return prisma.company_profiles.findMany(args as never);
  },

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

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

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

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

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

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

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

// ─── System Settings ────────────────────────────────────────────────────────

export const systemSettingsAdapter = {
  async findMany(args?: {
    where?: Record<string, unknown>;
    orderBy?: Record<string, unknown>;
  }) {
    return prisma.system_settings.findMany(args as never);
  },

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

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

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

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

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

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

// ─── Audit Logs ─────────────────────────────────────────────────────────────

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

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

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

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