/**
 * Stripe MCP Server - Subscriptions
 *
 * Provides Stripe subscription operations.
 * Token savings: 9,000+ per operation
 */

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

export interface StripeSubscription {
  id: string;
  customer: string;
  status: string;
  items: Array<{
    id: string;
    price: {
      id: string;
      recurring: {
        interval: string;
        interval_count: number;
      };
      unit_amount: number;
    };
  }>;
  current_period_start: number;
  current_period_end: number;
  cancel_at_period_end: boolean;
  metadata: Record<string, string>;
}

export interface ListSubscriptionsInput {
  customerId?: string;
  status?: string;
  limit?: number;
}

/**
 * Get Stripe subscription by ID.
 *
 * @param subscriptionId - Stripe subscription ID
 * @returns Subscription object
 *
 * @example
 * ```typescript
 * const sub = await getSubscription('sub_xxx');
 * console.log(`Status: ${sub.status}`);
 * console.log(`Price: ${sub.items[0].price.unit_amount / 100} SAR/${sub.items[0].price.recurring.interval}`);
 * ```
 */
export async function getSubscription(
  subscriptionId: string,
): Promise<StripeSubscription> {
  return callMCPTool<StripeSubscription>("stripe__get_subscription", {
    subscriptionId,
  });
}

/**
 * List Stripe subscriptions.
 *
 * @param input - Filter parameters
 * @returns Array of subscriptions
 *
 * @example
 * ```typescript
 * const subs = await listSubscriptions({ status: 'active', limit: 100 });
 *
 * // Calculate MRR in code
 * const mrr = subs.reduce((sum, sub) => {
 *   const price = sub.items[0]?.price;
 *   if (price?.recurring.interval === 'month') {
 *     return sum + (price.unit_amount / 100);
 *   }
 *   return sum;
 * }, 0);
 *
 * console.log(`MRR: ${mrr.toLocaleString()} SAR`);
 * ```
 */
export async function listSubscriptions(
  input: ListSubscriptionsInput = {},
): Promise<StripeSubscription[]> {
  return callMCPTool<StripeSubscription[]>("stripe__list_subscriptions", input);
}

/**
 * Cancel a subscription at period end.
 *
 * @param subscriptionId - Subscription ID to cancel
 * @returns Updated subscription
 *
 * @example
 * ```typescript
 * const cancelled = await cancelSubscription('sub_xxx');
 * console.log(`Cancelled: ${cancelled.cancel_at_period_end ? 'Yes' : 'No'}`);
 * console.log(`Active until: ${new Date(cancelled.current_period_end * 1000).toLocaleDateString()}`);
 * ```
 */
export async function cancelSubscription(
  subscriptionId: string,
): Promise<StripeSubscription> {
  return callMCPTool<StripeSubscription>("stripe__cancel_subscription", {
    subscriptionId,
  });
}

/**
 * Reactivate a cancelled subscription.
 *
 * @param subscriptionId - Subscription ID to reactivate
 * @returns Updated subscription
 *
 * @example
 * ```typescript
 * const reactivated = await reactivateSubscription('sub_xxx');
 * console.log('Subscription reactivated successfully');
 * ```
 */
export async function reactivateSubscription(
  subscriptionId: string,
): Promise<StripeSubscription> {
  return callMCPTool<StripeSubscription>("stripe__reactivate_subscription", {
    subscriptionId,
  });
}

export default {
  getSubscription,
  listSubscriptions,
  cancelSubscription,
  reactivateSubscription,
};
