/**
 * Config loader — parses process.env against the base schema + any extensions.
 */

import { type ZodRawShape, z } from 'zod';
import { baseEnvSchema } from './base-schema.js';

/**
 * Load and validate environment configuration.
 *
 * @param extension - Additional Zod fields specific to this service
 * @returns Parsed and validated config object
 *
 * @example
 * ```ts
 * const config = loadConfig({
 *   STRIPE_SECRET_KEY: z.string().min(1),
 *   BILLING_INTERNAL_KEY: z.string().optional(),
 * });
 * ```
 */
export function loadConfig<T extends ZodRawShape>(extension?: T) {
  const schema = extension ? baseEnvSchema.extend(extension) : baseEnvSchema;
  return schema.parse(process.env) as z.infer<typeof baseEnvSchema> &
    (T extends ZodRawShape ? z.infer<z.ZodObject<T>> : Record<string, never>);
}
