/**
 * WHMCS Payments Provider - Raw Types
 *
 * Type definitions for the WHMCS payment API contract:
 * - Request parameter types (API inputs)
 * - Response types (API outputs)
 */

import { z } from "zod";

// ============================================================================
// Request Parameter Types
// ============================================================================

/**
 * Parameters for WHMCS GetPayMethods API
 */
export interface WhmcsGetPayMethodsParams extends Record<string, unknown> {
  clientid: number;
  paymethodid?: number;
  type?: "BankAccount" | "CreditCard";
}

// ============================================================================
// Response Types
// ============================================================================

export const whmcsPaymentMethodRawSchema = z.object({
  id: z.number(),
  payment_type: z.string().optional(),
  type: z.string().optional(),
  description: z.string(),
  gateway_name: z.string().optional(),
  gateway: z.string().optional(),
  card_last_four: z.string().optional(),
  card_type: z.string().optional(),
  expiry_date: z.string().optional(),
  bank_name: z.string().optional(),
  remote_token: z.string().optional(),
  last_updated: z.string().optional(),
  is_default: z.union([z.boolean(), z.number(), z.string()]).optional(),
});

export type WhmcsPaymentMethodRaw = z.infer<typeof whmcsPaymentMethodRawSchema>;

export const whmcsPaymentGatewayRawSchema = z.object({
  name: z.string(),
  display_name: z.string().optional(),
  type: z.string(),
  visible: z.union([z.boolean(), z.number(), z.string()]).optional(),
  configuration: z.record(z.string(), z.unknown()).optional(),
});

export type WhmcsPaymentGatewayRaw = z.infer<typeof whmcsPaymentGatewayRawSchema>;

// ============================================================================
// WHMCS Payment Method List Response (GetPayMethods API)
// ============================================================================

/**
 * WHMCS payment method schema for list responses
 */
export const whmcsPaymentMethodSchema = z.object({
  id: z.number(),
  type: z.enum(["CreditCard", "BankAccount", "RemoteCreditCard", "RemoteBankAccount"]),
  description: z.string(),
  gateway_name: z.string().optional(),
  contact_type: z.string().optional(),
  contact_id: z.number().optional(),
  card_last_four: z.string().optional(),
  expiry_date: z.string().optional(),
  start_date: z.string().optional(),
  issue_number: z.string().optional(),
  card_type: z.string().optional(),
  remote_token: z.string().optional(),
  last_updated: z.string().optional(),
  bank_name: z.string().optional(),
});

export type WhmcsPaymentMethod = z.infer<typeof whmcsPaymentMethodSchema>;

/**
 * WHMCS GetPayMethods API response schema
 */
export const whmcsPaymentMethodListResponseSchema = z.object({
  clientid: z.union([z.number(), z.string()]),
  paymethods: z.array(whmcsPaymentMethodSchema).optional(),
  message: z.string().optional(),
});

export type WhmcsPaymentMethodListResponse = z.infer<typeof whmcsPaymentMethodListResponseSchema>;

// ============================================================================
// WHMCS Payment Gateway List Response (GetPaymentGateways API)
// ============================================================================

/**
 * WHMCS payment gateway schema for list responses
 */
export const whmcsPaymentGatewaySchema = z.object({
  name: z.string(),
  display_name: z.string(),
  type: z.enum(["merchant", "thirdparty", "tokenization", "manual"]),
  active: z.boolean(),
});

export type WhmcsPaymentGateway = z.infer<typeof whmcsPaymentGatewaySchema>;

/**
 * WHMCS GetPaymentGateways API response schema
 */
export const whmcsPaymentGatewayListResponseSchema = z.object({
  gateways: z.object({
    gateway: z.array(whmcsPaymentGatewaySchema),
  }),
  totalresults: z.number(),
});

export type WhmcsPaymentGatewayListResponse = z.infer<typeof whmcsPaymentGatewayListResponseSchema>;
