/**
 * Checkout Domain - Schemas
 *
 * Zod validation schemas for checkout flow.
 * Supports authenticated checkout.
 */

import { z } from "zod";
import { CHECKOUT_ORDER_TYPE } from "./contract.js";

// ============================================================================
// Order Type Schema
// ============================================================================

const CHECKOUT_ORDER_TYPE_VALUES = Object.values(CHECKOUT_ORDER_TYPE) as [string, ...string[]];

/**
 * Checkout order types - uses PascalCase to match Salesforce/BFF contracts
 * @see packages/domain/orders/contract.ts ORDER_TYPE for canonical values
 */
export const checkoutOrderTypeSchema = z.enum(CHECKOUT_ORDER_TYPE_VALUES);

// ============================================================================
// Price Breakdown Schema
// ============================================================================

export const priceBreakdownItemSchema = z.object({
  label: z.string(),
  sku: z.string().optional(),
  monthlyPrice: z.number().optional(),
  oneTimePrice: z.number().optional(),
  quantity: z.number().optional().default(1),
});

// ============================================================================
// Cart Item Schema
// ============================================================================

export const cartItemSchema = z.object({
  orderType: checkoutOrderTypeSchema,
  planSku: z.string().min(1, "Plan SKU is required"),
  planName: z.string().min(1, "Plan name is required"),
  addonSkus: z.array(z.string()).default([]),
  // Checkout configuration values are user-supplied key-value pairs (strings, numbers, booleans)
  configuration: z
    .record(z.string(), z.union([z.string(), z.number(), z.boolean(), z.null()]))
    .default({}),
  pricing: z.object({
    monthlyTotal: z.number().nonnegative(),
    oneTimeTotal: z.number().nonnegative(),
    breakdown: z.array(priceBreakdownItemSchema).default([]),
  }),
});

// ============================================================================
// Inferred Types
// ============================================================================

export type OrderType = z.infer<typeof checkoutOrderTypeSchema>;
export type PriceBreakdownItem = z.infer<typeof priceBreakdownItemSchema>;
export type CartItem = z.infer<typeof cartItemSchema>;
