/**
 * Common Salesforce Provider Types
 * 
 * Generic Salesforce API response structures used across multiple domains.
 */

import { z } from "zod";

// ============================================================================
// Salesforce Query Response (Generic SOQL Response)
// ============================================================================

/**
 * Base schema for Salesforce SOQL query result
 */
const salesforceResponseBaseSchema = z.object({
  totalSize: z.number(),
  done: z.boolean(),
  records: z.array(z.unknown()),
});

type SalesforceResponseBase = z.infer<typeof salesforceResponseBaseSchema>;

/**
 * Generic type for Salesforce query results derived from schema
 * All SOQL queries return this structure regardless of SObject type
 * 
 * Usage: SalesforceResponse<SalesforceOrderRecord>
 */
export type SalesforceResponse<TRecord> = Omit<SalesforceResponseBase, 'records'> & {
  records: TRecord[];
};

/**
 * Schema factory for validating Salesforce query responses
 * Usage: salesforceResponseSchema(salesforceOrderRecordSchema)
 */
export const salesforceResponseSchema = <TRecord extends z.ZodTypeAny>(recordSchema: TRecord) =>
  salesforceResponseBaseSchema.extend({
    records: z.array(recordSchema),
  });

