/**
 * Types for @bytelyst/subscription-client.
 * Browser/React Native-safe — no Node.js dependencies.
 */

export interface SubscriptionDoc {
  id: string;
  productId: string;
  userId: string;
  plan: 'free' | 'pro' | 'enterprise';
  status: 'active' | 'cancelled' | 'past_due' | 'trialing';
  currentPeriodStart: string;
  currentPeriodEnd: string;
  cancelAtPeriodEnd: boolean;
  monthlyPrice: number;
  tokensIncluded: number;
  tokensUsed: number;
  stripeCustomerId?: string;
  stripeSubscriptionId?: string;
  createdAt: string;
  updatedAt: string;
}

export interface PlanConfig {
  id: string;
  productId: string;
  name: string;
  displayName: string;
  price: number;
  tokens: number;
  words: number;
  dictations: number;
  features: string[];
  stripePriceId?: string;
  active: boolean;
  createdAt: string;
  updatedAt: string;
}

export interface SubscriptionClientConfig {
  /** Platform-service base URL (e.g. "http://localhost:4003/api"). */
  baseUrl: string;

  /** Product identifier sent as x-product-id header on every request. */
  productId: string;

  /** User ID — subscription routes are keyed by userId, not id. */
  userId: string;

  /** Returns a JWT access token, or null if not authenticated. */
  getAccessToken: () => string | null;

  /** Optional persistent storage adapter for cache. */
  storage?: {
    getItem(key: string): string | null;
    setItem(key: string, value: string): void;
  };
}

export interface SubscriptionClient {
  // Server-authoritative checks
  getMySubscription(): Promise<SubscriptionDoc | null>;
  getPlans(): Promise<PlanConfig[]>;
  startTrial(planName?: string): Promise<SubscriptionDoc>;
  cancelSubscription(): Promise<SubscriptionDoc>;
  updateSubscription(updates: Partial<SubscriptionDoc>): Promise<SubscriptionDoc>;

  // Client-side helpers (cached, offline-safe)
  isPro(): boolean;
  isTrialing(): boolean;
  hasFeature(feature: string): boolean;
  daysRemaining(): number | null;
  getCachedSubscription(): SubscriptionDoc | null;
  getCachedPlans(): PlanConfig[];
  refresh(): Promise<void>;
}
