import { createMixpanelService, type MixpanelConfig } from "./mixpanel-service";
import { createGA4Service, type GA4Config } from "./ga4-service";
import { createStripeService, type StripeDashboardConfig } from "./stripe-service";

export interface AnalyticsConfig {
  mixpanel: MixpanelConfig;
  ga4: GA4Config;
  stripe: StripeDashboardConfig;
}

export class AnalyticsConfigManager {
  private config: AnalyticsConfig;
  private mixpanelService?: ReturnType<typeof createMixpanelService>;
  private ga4Service?: ReturnType<typeof createGA4Service>;
  private stripeService?: ReturnType<typeof createStripeService>;

  constructor(config: AnalyticsConfig) {
    this.config = config;
  }

  initialize(): void {
    // Initialize Mixpanel
    this.mixpanelService = createMixpanelService(this.config.mixpanel);

    // Initialize GA4
    this.ga4Service = createGA4Service(this.config.ga4);
    this.ga4Service.initialize();

    // Initialize Stripe
    this.stripeService = createStripeService(this.config.stripe);
    this.stripeService.initialize();
  }

  getMixpanel(): ReturnType<typeof createMixpanelService> {
    if (!this.mixpanelService) {
      throw new Error("Analytics not initialized. Call initialize() first.");
    }
    return this.mixpanelService;
  }

  getGA4(): ReturnType<typeof createGA4Service> {
    if (!this.ga4Service) {
      throw new Error("Analytics not initialized. Call initialize() first.");
    }
    return this.ga4Service;
  }

  getStripe(): ReturnType<typeof createStripeService> {
    if (!this.stripeService) {
      throw new Error("Analytics not initialized. Call initialize() first.");
    }
    return this.stripeService;
  }

  getConfig(): AnalyticsConfig {
    return this.config;
  }
}

export const loadAnalyticsConfig = (): AnalyticsConfig => {
  return {
    mixpanel: {
      projectToken: import.meta.env.VITE_MIXPANEL_PROJECT_TOKEN || "",
      options: {
        debug: import.meta.env.VITE_MIXPANEL_DEBUG === "true",
        track_pageview: true,
        persistence: "localStorage",
      },
    },
    ga4: {
      measurementId: import.meta.env.VITE_GA4_MEASUREMENT_ID || "",
      options: {
        debug: import.meta.env.VITE_GA4_DEBUG === "true",
        autoTrackPageViews: true,
        autoTrackScrolls: true,
        autoTrackOutboundLinks: true,
      },
    },
    stripe: {
      publishableKey: import.meta.env.VITE_STRIPE_PUBLISHABLE_KEY || "",
      secretKey: process.env.STRIPE_SECRET_KEY || "",
      webhookSecret: process.env.STRIPE_WEBHOOK_SECRET || "",
      apiVersion: "2024-12-18.acacia",
    },
  };
};

export const loadStripeServerConfig = () => {
  return {
    secretKey: process.env.STRIPE_SECRET_KEY || "",
    webhookSecret: process.env.STRIPE_WEBHOOK_SECRET || "",
    apiVersion: "2024-12-18.acacia",
  };
};

export const createAnalyticsManager = (): AnalyticsConfigManager => {
  const config = loadAnalyticsConfig();
  const manager = new AnalyticsConfigManager(config);
  manager.initialize();
  return manager;
};
