/**
 * Stripe MCP Server - Customers
 *
 * Provides Stripe customer management operations.
 * Token savings: 8,000+ per operation (large customer objects)
 */

import { callMCPTool } from "../../client.js";

export interface StripeCustomer {
  id: string;
  email: string;
  name: string | null;
  metadata: Record<string, string>;
  created: number;
  subscriptions: any[];
}

export interface CreateCustomerInput {
  email: string;
  name?: string;
  metadata?: Record<string, string>;
}

export interface ListCustomersInput {
  limit?: number;
  startingAfter?: string;
}

/**
 * Get Stripe customer by ID.
 *
 * @param customerId - Stripe customer ID
 * @returns Customer object
 *
 * @example
 * ```typescript
 * const customer = await getCustomer('cus_xxx');
 * console.log(`Customer: ${customer.email}`);
 * console.log(`Subscriptions: ${customer.subscriptions.length}`);
 * ```
 */
export async function getCustomer(customerId: string): Promise<StripeCustomer> {
  return callMCPTool<StripeCustomer>("stripe__get_customer", { customerId });
}

/**
 * List Stripe customers with pagination.
 *
 * @param input - Pagination parameters
 * @returns Array of customer objects
 *
 * @example
 * ```typescript
 * const customers = await listCustomers({ limit: 50 });
 *
 * // Analyze customer data in code
 * const withSubs = customers.filter(c => c.subscriptions.length > 0);
 * console.log(`${withSubs.length}/${customers.length} have subscriptions`);
 * ```
 */
export async function listCustomers(
  input: ListCustomersInput = {},
): Promise<StripeCustomer[]> {
  return callMCPTool<StripeCustomer[]>("stripe__list_customers", input);
}

/**
 * Create a new Stripe customer.
 *
 * @param input - Customer data
 * @returns Created customer
 *
 * @example
 * ```typescript
 * const customer = await createCustomer({
 *   email: 'new@example.com',
 *   name: 'John Doe',
 *   metadata: { userId: 'user_123' }
 * });
 * console.log(`Customer created: ${customer.id}`);
 * ```
 */
export async function createCustomer(
  input: CreateCustomerInput,
): Promise<StripeCustomer> {
  return callMCPTool<StripeCustomer>("stripe__create_customer", input);
}

/**
 * Get customer by email.
 *
 * @param email - Customer email
 * @returns Customer object or null
 *
 * @example
 * ```typescript
 * const customer = await getCustomerByEmail('user@example.com');
 * if (customer) {
 *   console.log(`Found customer: ${customer.id}`);
 * }
 * ```
 */
export async function getCustomerByEmail(
  email: string,
): Promise<StripeCustomer | null> {
  return callMCPTool<StripeCustomer | null>("stripe__get_customer_by_email", {
    email,
  });
}

export default {
  getCustomer,
  listCustomers,
  createCustomer,
  getCustomerByEmail,
};
