import "server-only";

import { Query } from "node-appwrite";

import { DATABASE_ID, TABLES, type ClinicPricing } from "./schema";
import { createAdminClient } from "./server";
import { toPlain } from "./serialize";

export async function listClinicPricing(
  labTenantId: string,
  clinicTenantId: string,
): Promise<ClinicPricing[]> {
  const { tablesDB } = createAdminClient();
  const result = await tablesDB.listRows({
    databaseId: DATABASE_ID,
    tableId: TABLES.clinicPricing,
    queries: [
      Query.equal("labTenantId", labTenantId),
      Query.equal("clinicTenantId", clinicTenantId),
      Query.limit(50),
    ],
  });
  return toPlain(result.rows as unknown as ClinicPricing[]);
}

export async function listClinicPricingForClinic(
  clinicTenantId: string,
  labTenantId: string,
): Promise<ClinicPricing[]> {
  return listClinicPricing(labTenantId, clinicTenantId);
}
