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

import { z } from "zod";

// ============================================================================
// WHMCS Generic Response Wrapper
// ============================================================================

/**
 * Base schema for WHMCS API response wrapper
 */
const whmcsResponseBaseSchema = z.object({
  result: z.enum(["success", "error"]),
  message: z.string().optional(),
});

type WhmcsResponseBase = z.infer<typeof whmcsResponseBaseSchema>;

/**
 * Generic type for WHMCS API responses derived from schema
 * All WHMCS API endpoints return this structure
 * 
 * Usage: WhmcsResponse<InvoiceData>
 */
export type WhmcsResponse<T> = WhmcsResponseBase & {
  data?: T;
};

/**
 * Schema factory for validating WHMCS responses
 * Usage: whmcsResponseSchema(invoiceSchema)
 */
export const whmcsResponseSchema = <T extends z.ZodTypeAny>(dataSchema: T) =>
  whmcsResponseBaseSchema.extend({
    data: dataSchema.optional(),
  });

// ============================================================================
// WHMCS Error Response
// ============================================================================

/**
 * WHMCS error response schema
 */
export const whmcsErrorResponseSchema = z.object({
  result: z.literal("error"),
  message: z.string(),
  errorcode: z.string().optional(),
});

export type WhmcsErrorResponse = z.infer<typeof whmcsErrorResponseSchema>;

