export type SubscriptionTier = 'BASIC' | 'PLUS' | 'PREMIUM';

export interface TierRateLimits {
  perMinute: number;
  perDay: number;
}

export type SubscriptionTierRateLimits = Record<SubscriptionTier, TierRateLimits>;

export const spamRateLimits: SubscriptionTierRateLimits = {
  BASIC: { perMinute: 100, perDay: 1000 },
  PLUS: { perMinute: 500, perDay: 5000 },
  PREMIUM: { perMinute: 2000, perDay: 20000 },
} as const;

export const spamFeatureFlagDefaults = {
  enableHiyaIntegration: true,
  enableTruecallerIntegration: true,
  enableSMSClassification: true,
  enableCallAnalysis: true,
  enableFeedbackLoop: true,
} as const;

type FeatureFlagKey = keyof typeof spamFeatureFlagDefaults;

export function checkFeatureFlag(flag: FeatureFlagKey): boolean {
  const envKey = `FLAG_${flag.toUpperCase()}`;
  const envValue = process.env[envKey];

  if (envValue !== undefined) {
    return envValue === 'true' || envValue === '1';
  }

  return spamFeatureFlagDefaults[flag];
}

export const spamFeatureFlags = {
  get enableHiyaIntegration() {
    return checkFeatureFlag('enableHiyaIntegration');
  },
  get enableTruecallerIntegration() {
    return checkFeatureFlag('enableTruecallerIntegration');
  },
  get enableSMSClassification() {
    return checkFeatureFlag('enableSMSClassification');
  },
  get enableCallAnalysis() {
    return checkFeatureFlag('enableCallAnalysis');
  },
  get enableFeedbackLoop() {
    return checkFeatureFlag('enableFeedbackLoop');
  },
} as const;

export const spamConfig = {
  maxPhoneNumberLength: 20,
  minPhoneNumberLength: 10,
  defaultConfidenceThreshold: 0.7,
  maxMetadataSize: 1024 * 4,
  circuitBreakerThreshold: 5,
  circuitBreakerTimeout: 60000,
} as const;

export const metadataLimits = {
  maxMetadataSizeBytes: 4096,
  maxMetadataKeys: 20,
  maxMetadataValueSizeBytes: 512,
} as const;

/** Reputation and Spam Score Constants */
export const defaultReputationConfidence = 0.7;
export const defaultSpamScore = 0.0;
export const highReputationThreshold = 0.8;
export const lowReputationThreshold = 0.3;

/** Feature Weights for Reputation Scoring */
export const featureWeights = {
  reputationWeight: 0.4,
  ruleWeight: 0.3,
  behavioralWeight: 0.2,
  userHistoryWeight: 0.1,
} as const;
