/**
 * Orders Domain - WHMCS Provider Raw Types
 * 
 * Raw types for WHMCS API request/response.
 * 
 * WHMCS AddOrder API:
 * @see https://developers.whmcs.com/api-reference/addorder/
 * Adds an order to a client. Returns orderid, serviceids, addonids, domainids, invoiceid.
 * CRITICAL: Must include qty[] parameter or no products will be added to the order!
 * 
 * WHMCS AcceptOrder API:
 * @see https://developers.whmcs.com/api-reference/acceptorder/
 * Accepts a pending order. Parameters:
 * - orderid (int): Required - The order ID to accept
 * - autosetup (bool): Optional - Send request to product module to activate service
 * - sendemail (bool): Optional - Send automatic emails (Product Welcome, etc.)
 * - registrar (string): Optional - Specific registrar for domains
 * - sendregistrar (bool): Optional - Send request to registrar
 * Returns: { result: "success" } on success
 */

import { z } from "zod";

// ============================================================================
// WHMCS Order Item Schema
// ============================================================================

export const whmcsOrderItemSchema = z.object({
  productId: z.string().min(1, "Product ID is required"), // WHMCS Product ID
  billingCycle: z.enum([
    "monthly",
    "quarterly",
    "semiannually",
    "annually",
    "biennially",
    "triennially",
    "onetime",
    "free"
  ]),
  quantity: z.number().int().positive("Quantity must be positive").default(1),
  configOptions: z.record(z.string(), z.string()).optional(),
  customFields: z.record(z.string(), z.string()).optional(),
});

export type WhmcsOrderItem = z.infer<typeof whmcsOrderItemSchema>;

// ============================================================================
// WHMCS AddOrder API Parameters Schema
// ============================================================================

export const whmcsAddOrderParamsSchema = z.object({
  clientId: z.number().int().positive("Client ID must be positive"),
  items: z.array(whmcsOrderItemSchema).min(1, "At least one item is required"),
  paymentMethod: z.string().min(1, "Payment method is required"),
  promoCode: z.string().optional(),
  notes: z.string().optional(),
  sfOrderId: z.string().optional(), // For tracking back to Salesforce
  noinvoice: z.boolean().optional(), // Don't create invoice during provisioning
  noinvoiceemail: z.boolean().optional(), // Suppress invoice email
  noemail: z.boolean().optional(), // Don't send any emails
});

export type WhmcsAddOrderParams = z.infer<typeof whmcsAddOrderParamsSchema>;

// ============================================================================
// WHMCS AddOrder API Payload Schema
// ============================================================================

export const whmcsAddOrderPayloadSchema = z.object({
  clientid: z.number().int().positive(),
  paymentmethod: z.string().min(1),
  promocode: z.string().optional(),
  notes: z.string().optional(),
  noinvoice: z.boolean().optional(),
  noinvoiceemail: z.boolean().optional(),
  noemail: z.boolean().optional(),
  pid: z.array(z.string()).min(1),
  billingcycle: z.array(z.string()).min(1),
  qty: z.array(z.number().int().positive()).min(1),
  configoptions: z.array(z.string()).optional(), // base64 encoded
  customfields: z.array(z.string()).optional(), // base64 encoded
});

export type WhmcsAddOrderPayload = z.infer<typeof whmcsAddOrderPayloadSchema>;

// ============================================================================
// WHMCS Order Result Schema
// ============================================================================

export const whmcsAddOrderResponseSchema = z.object({
  orderid: z.union([z.string(), z.number()]),
  invoiceid: z.union([z.string(), z.number()]).optional(),
  serviceids: z.string().optional(),
  addonids: z.string().optional(),
  domainids: z.string().optional(),
});

export type WhmcsAddOrderResponse = z.infer<typeof whmcsAddOrderResponseSchema>;

export const whmcsOrderResultSchema = z.object({
  orderId: z.number().int().positive(),
  invoiceId: z.number().int().positive().optional(),
  serviceIds: z.array(z.number().int().positive()).default([]),
  addonIds: z.array(z.number().int().positive()).default([]),
  domainIds: z.array(z.number().int().positive()).default([]),
});

export type WhmcsOrderResult = z.infer<typeof whmcsOrderResultSchema>;

// ============================================================================
// WHMCS AcceptOrder API Response Schema
// ============================================================================

export const whmcsAcceptOrderResponseSchema = z.object({
  orderid: z.union([z.string(), z.number()]).optional(),
  invoiceid: z.union([z.string(), z.number()]).optional(),
  serviceids: z.string().optional(),
  addonids: z.string().optional(),
  domainids: z.string().optional(),
});

export type WhmcsAcceptOrderResponse = z.infer<typeof whmcsAcceptOrderResponseSchema>;
