/**
 * Stripe API Types
 *
 * Type definitions for Stripe Payment Links operations.
 */

/**
 * Stripe credentials format
 * Stored in credentials table, encrypted
 */
export interface StripeCredentials {
  apiKey: string // sk_live_xxx or sk_test_xxx
}

/**
 * Create payment link parameters
 */
export interface CreatePaymentLinkParams {
  priceId: string // Stripe price ID (price_xxx)
  quantity?: number // Default: 1
  metadata?: Record<string, string> // Custom metadata
  afterCompletionType?: 'redirect' | 'hosted_confirmation' // What to show after payment
  redirectUrl?: string // Required if afterCompletionType is 'redirect'
  allowPromotionCodes?: boolean // Allow promo codes at checkout
  automaticTax?: boolean // Enable automatic tax calculation
  billingAddressCollection?: 'auto' | 'required' // Billing address requirement
  invoiceCreation?: boolean // Generate invoice for payment
}

/**
 * Create payment link result
 */
export interface CreatePaymentLinkResult {
  id: string // plink_xxx
  url: string // https://checkout.stripe.com/...
  active: boolean
  livemode: boolean
}

/**
 * Get payment link parameters
 */
export interface GetPaymentLinkParams {
  paymentLinkId: string // Payment link ID to retrieve
}

/**
 * Get payment link result
 */
export interface GetPaymentLinkResult {
  id: string
  url: string
  active: boolean
  livemode: boolean
  metadata: Record<string, string>
}

/**
 * Update payment link parameters
 */
export interface UpdatePaymentLinkParams {
  paymentLinkId: string // Payment link ID to update
  active?: boolean // Enable or disable the link
  metadata?: Record<string, string> // Update metadata
}

/**
 * Update payment link result
 */
export interface UpdatePaymentLinkResult {
  id: string
  active: boolean
  url: string
}

/**
 * List payment links parameters
 */
export interface ListPaymentLinksParams {
  active?: boolean // Filter by active status
  limit?: number // Default: 10, max: 100
  startingAfter?: string // Cursor for pagination (payment link ID)
  endingBefore?: string // Cursor for pagination (payment link ID)
}

/**
 * List payment links result
 */
export interface ListPaymentLinksResult {
  data: Array<{
    id: string
    url: string
    active: boolean
    metadata: Record<string, string>
  }>
  hasMore: boolean
}

/**
 * Stripe API response for create payment link
 */
export interface StripeCreatePaymentLinkResponse {
  id: string
  url: string
  active: boolean
  livemode: boolean
}

/**
 * Stripe API response for get payment link
 */
export interface StripeGetPaymentLinkResponse {
  id: string
  url: string
  active: boolean
  livemode: boolean
  metadata: Record<string, string>
}

/**
 * Stripe API response for list payment links
 */
export interface StripeListPaymentLinksResponse {
  object: 'list'
  data: Array<{
    id: string
    url: string
    active: boolean
    metadata: Record<string, string>
  }>
  has_more: boolean
}

// =============================================================================
// Auto Payment Links (Dynamic Pricing)
// =============================================================================

/**
 * Create auto payment link parameters
 *
 * For dynamic pricing with inline price_data (one-time + recurring).
 * Unlike standard payment links, these don't require pre-created Price objects.
 */
export interface CreateAutoPaymentLinkParams {
  /** Local deal ID for metadata */
  dealId: string
  /** Company name for product description */
  companyName: string
  /** Contact email for metadata */
  contactEmail: string
  /** One-time setup fee in dollars (e.g., 3000 for $3,000) */
  initialFee: number
  /** Recurring monthly fee in dollars (e.g., 500 for $500/month) */
  monthlyFee: number
}

/**
 * Create auto payment link result
 */
export interface CreateAutoPaymentLinkResult {
  /** Payment link ID (plink_xxx) */
  paymentLinkId: string
  /** Payment URL (NEVER EXPIRES) */
  url: string
  /** Whether link includes one-time fee */
  hasInitialFee: boolean
  /** Whether link includes recurring subscription */
  hasRecurring: boolean
}

// =============================================================================
// Checkout Sessions
// =============================================================================

/**
 * Create checkout session parameters
 *
 * For variable/custom pricing where the amount comes from the proposal.
 * Uses price_data instead of a pre-defined price ID.
 */
export interface CreateCheckoutSessionParams {
  /** Amount in cents (e.g., 2400000 for $24,000) */
  amount: number
  /** Currency code (default: 'usd') */
  currency?: string
  /** Product name shown in checkout (default: 'AI Automation Services') */
  productName?: string
  /** Product description shown in checkout */
  productDescription?: string
  /** Metadata for webhook lookup (deal_id, etc.) */
  metadata?: Record<string, string>
  /** URL after successful payment */
  successUrl: string
  /** URL if customer cancels */
  cancelUrl: string
  /** Pre-fill customer email in checkout */
  customerEmail?: string
}

/**
 * Create checkout session result
 */
export interface CreateCheckoutSessionResult {
  /** Session ID (cs_xxx) */
  id: string
  /** Payment URL (expires in 24 hours) */
  url: string
  /** Expiration timestamp (Unix seconds) */
  expiresAt: number
}
