/**
 * Integration Catalog
 * ---------------------
 * Metadata for every integration provider meSEO supports.
 * Used by the frontend to display connection cards and by the
 * backend to know which env vars and scopes each integration needs.
 *
 * To add a new integration:
 *   1. Add an entry here
 *   2. Create a service file (e.g. src/lib/integrations/my-service.ts)
 *   3. Register it in index.ts syncServices
 */

export interface CatalogEntry {
  id: string;
  name: string;
  description: string;
  category: "analytics" | "search" | "local" | "performance" | "ai" | "ads";
  /** Which env var holds the API key (if key-based auth) */
  envKey?: string;
  /** Required OAuth scopes (if OAuth-based auth) */
  scopes?: string[];
  /** Whether this integration uses OAuth vs API key */
  authType: "oauth" | "api_key" | "none";
  /** The base URL of the provider's API */
  apiBaseUrl: string;
  /** What data this integration provides */
  dataProvided: string[];
  /** Setup instructions shown to the user */
  setupNote: string;
}

export const INTEGRATION_CATALOG: Record<string, CatalogEntry> = {
  gsc: {
    id: "gsc",
    name: "Google Search Console",
    description: "Search keywords, clicks, impressions, and indexing status",
    category: "search",
    authType: "oauth",
    scopes: ["https://www.googleapis.com/auth/webmasters.readonly"],
    apiBaseUrl: "https://www.googleapis.com/webmasters/v3",
    dataProvided: ["search_queries", "clicks", "impressions", "ctr", "position", "indexing"],
    setupNote: "Requires Google OAuth. The website must be verified in GSC.",
  },
  ga4: {
    id: "ga4",
    name: "Google Analytics 4",
    description: "Traffic, sessions, users, and conversion events",
    category: "analytics",
    authType: "oauth",
    scopes: ["https://www.googleapis.com/auth/analytics.readonly"],
    apiBaseUrl: "https://analyticsdata.googleapis.com/v1beta",
    dataProvided: ["sessions", "users", "pageviews", "events", "conversions", "bounce_rate"],
    setupNote: "Requires Google OAuth. Needs a GA4 property ID.",
  },
  gbp: {
    id: "gbp",
    name: "Google Business Profile",
    description: "Local visibility, reviews, and map pack insights",
    category: "local",
    authType: "oauth",
    scopes: ["https://www.googleapis.com/auth/business.manage"],
    apiBaseUrl: "https://mybusinessbusinessinformation.googleapis.com/v1",
    dataProvided: ["reviews", "ratings", "local_visibility", "map_pack", "photos", "posts"],
    setupNote: "Requires Google OAuth. The business must be claimed in GBP.",
  },
  pagespeed: {
    id: "pagespeed",
    name: "Google PageSpeed / Lighthouse",
    description: "Core Web Vitals, performance scores, and optimization tips",
    category: "performance",
    envKey: "GOOGLE_PAGESPEED_API_KEY",
    authType: "api_key",
    apiBaseUrl: "https://www.googleapis.com/pagespeedonline/v5",
    dataProvided: ["performance_score", "lcp", "fid", "cls", "fcp", "ttfb", "opportunities"],
    setupNote: "Uses a Google API key. No OAuth needed. Free tier: 25,000 requests/day.",
  },
  bing: {
    id: "bing",
    name: "Bing Webmaster Tools",
    description: "Bing search data, crawl info, and keyword rankings",
    category: "search",
    envKey: "BING_WEBMASTER_API_KEY",
    authType: "api_key",
    apiBaseUrl: "https://ssl.bing.com/webmaster/api.svc/json",
    dataProvided: ["search_queries", "clicks", "impressions", "crawl_stats", "seo_issues"],
    setupNote: "Requires a Bing Webmaster API key from the Bing portal.",
  },
  claude: {
    id: "claude",
    name: "Claude (Anthropic)",
    description: "AI-powered strategist for SEO insights and recommendations",
    category: "ai",
    envKey: "ANTHROPIC_API_KEY",
    authType: "api_key",
    apiBaseUrl: "https://api.anthropic.com/v1",
    dataProvided: ["ai_insights", "recommendations", "content_analysis", "strategy"],
    setupNote: "Set ANTHROPIC_API_KEY in your environment. Powers the AI Avatar.",
  },
};

/**
 * Get the catalog entry for an integration.
 */
export function getCatalogEntry(integrationId: string): CatalogEntry | null {
  return INTEGRATION_CATALOG[integrationId] ?? null;
}

/**
 * Check if an integration's API key is configured in the environment.
 */
export function isKeyConfigured(integrationId: string): boolean {
  const entry = INTEGRATION_CATALOG[integrationId];
  if (!entry) return false;
  if (entry.authType === "oauth") return true; // OAuth doesn't use env keys
  if (!entry.envKey) return false;
  return !!process.env[entry.envKey];
}
