interface ValidationInfo {
  message: string;
  action?: string;
  actionUrl?: string;
}

const PROVIDER_META: Record<string, { label: string; docsUrl: string; billingUrl: string; statusUrl: string; keysUrl: string }> = {
  anthropic: {
    label: 'Anthropic',
    docsUrl: 'https://docs.anthropic.com/en/api/rate-limits',
    billingUrl: 'https://console.anthropic.com/settings/plans',
    statusUrl: 'https://status.anthropic.com',
    keysUrl: 'https://console.anthropic.com/settings/keys',
  },
  openai: {
    label: 'OpenAI',
    docsUrl: 'https://platform.openai.com/docs/guides/rate-limits',
    billingUrl: 'https://platform.openai.com/account/billing/overview',
    statusUrl: 'https://status.openai.com',
    keysUrl: 'https://platform.openai.com/api-keys',
  },
};

// Canonical AI key validation states. Key set must match SerpKey's `validationInfoMap`
// — a drift guard in SerpKey.test.tsx asserts the two stay aligned.
export const validationInfoMap: Record<string, ValidationInfo> = {
  valid: {
    message: 'Your key is active and ready to generate content.',
  },
  invalid_auth: {
    message:
      'This key was rejected by OpenAI. It may be expired, revoked, or mistyped. Replace it with a fresh key from your OpenAI dashboard.',
    action: 'Get a new key',
    actionUrl: 'https://platform.openai.com/api-keys',
  },
  quota_exceeded: {
    message:
      'Your OpenAI account has run out of credits or hit a billing limit. Add a payment method or increase your spend limit to resume AI generation.',
    action: 'Manage OpenAI billing',
    actionUrl: 'https://platform.openai.com/account/billing/overview',
  },
  rate_limited: {
    message:
      'OpenAI is throttling requests from your account. This is usually temporary — wait a few minutes and test again.',
    action: 'Check rate limit docs',
    actionUrl: 'https://platform.openai.com/docs/guides/rate-limits',
  },
  network_error: {
    message:
      'RankWizAI could not reach the OpenAI API. This is usually a temporary network issue. Try validating again in a few minutes.',
  },
  provider_error: {
    message:
      'OpenAI returned an unexpected error. Check the OpenAI status page to see if there is an ongoing incident.',
    action: 'OpenAI status page',
    actionUrl: 'https://status.openai.com',
  },
};

/**
 * Returns provider-specific validation info for a given status.
 * Use this instead of validationInfoMap when the provider is known (e.g. inside stored_keys.map()).
 */
export function getValidationInfo(status: string, provider: string): ValidationInfo | null {
  const meta = PROVIDER_META[provider] ?? PROVIDER_META.openai;

  switch (status) {
    case 'valid':
      return { message: 'Your key is active and ready to generate content.' };
    case 'invalid_auth':
      return {
        message: `This key was rejected by ${meta.label}. It may be expired, revoked, or mistyped. Replace it with a fresh key from your ${meta.label} dashboard.`,
        action: 'Get a new key',
        actionUrl: meta.keysUrl,
      };
    case 'quota_exceeded':
      return {
        message: `Your ${meta.label} account has run out of credits or hit a billing limit. Add a payment method or increase your spend limit to resume AI generation.`,
        action: `Manage ${meta.label} billing`,
        actionUrl: meta.billingUrl,
      };
    case 'rate_limited':
      return {
        message: `${meta.label} is throttling requests from your account. This is usually temporary — wait a few minutes and test again.`,
        action: 'Check rate limit docs',
        actionUrl: meta.docsUrl,
      };
    case 'network_error':
      return {
        message: `RankWizAI could not reach the ${meta.label} API. This is usually a temporary network issue. Try validating again in a few minutes.`,
      };
    case 'provider_error':
      return {
        message: `${meta.label} returned an unexpected error. Check the ${meta.label} status page to see if there is an ongoing incident.`,
        action: `${meta.label} status page`,
        actionUrl: meta.statusUrl,
      };
    default:
      return validationInfoMap[status] ?? null;
  }
}
