export interface UrlCheckResult {
  url: string;
  domain: string;
  verdict: UrlVerdict;
  confidence: number;
  threats: ThreatInfo[];
  cached: boolean;
  latencyMs: number;
  timestamp: number;
}

export enum UrlVerdict {
  SAFE = 'safe',
  SUSPICIOUS = 'suspicious',
  PHISHING = 'phishing',
  SPAM = 'spam',
  EXPOSED_CREDENTIALS = 'exposed_credentials',
  UNKNOWN = 'unknown',
}

export interface ThreatInfo {
  type: ThreatType;
  severity: number;
  source: string;
  description: string;
}

export enum ThreatType {
  PHISHING_KNOWN = 'phishing_known',
  PHISHING_HEURISTIC = 'phishing_heuristic',
  DOMAIN_AGE = 'domain_age',
  SSL_ANOMALY = 'ssl_anomaly',
  URL_ENTROPY = 'url_entropy',
  TYPOSQUAT = 'typosquat',
  CREDENTIAL_EXPOSURE = 'credential_exposure',
  SPAM_SOURCE = 'spam_source',
  REDIRECT_CHAIN = 'redirect_chain',
  MIXED_CONTENT = 'mixed_content',
}

export interface CachedUrlEntry {
  url: string;
  result: UrlCheckResult;
  expiresAt: number;
}

export interface ExtensionSettings {
  apiKey: string;
  apiBaseUrl: string;
  authToken: string | null;
  userId: string | null;
  tier: SubscriptionTier | null;
  enabled: boolean;
  activeBlocking: boolean;
  darkWatchEnabled: boolean;
  spamProtectionEnabled: boolean;
  showNotifications: boolean;
  blockedDomains: string[];
  allowedDomains: string[];
  lastSyncAt: number | null;
}

export enum SubscriptionTier {
  BASIC = 'basic',
  PLUS = 'plus',
  PREMIUM = 'premium',
}

export interface TierFeatures {
  passiveWarnings: boolean;
  activeBlocking: boolean;
  darkWatchIntegration: boolean;
  realTimeScanning: boolean;
  maxDailyChecks: number;
}

export const TIER_FEATURES: Record<SubscriptionTier, TierFeatures> = {
  [SubscriptionTier.BASIC]: {
    passiveWarnings: true,
    activeBlocking: false,
    darkWatchIntegration: false,
    realTimeScanning: false,
    maxDailyChecks: 100,
  },
  [SubscriptionTier.PLUS]: {
    passiveWarnings: true,
    activeBlocking: true,
    darkWatchIntegration: true,
    realTimeScanning: false,
    maxDailyChecks: 1000,
  },
  [SubscriptionTier.PREMIUM]: {
    passiveWarnings: true,
    activeBlocking: true,
    darkWatchIntegration: true,
    realTimeScanning: true,
    maxDailyChecks: 10000,
  },
};

export interface PhishingReport {
  url: string;
  pageTitle: string;
  tabId: number;
  timestamp: number;
  reason: string;
  heuristics: Record<string, unknown>;
}

export interface PopupData {
  protectionEnabled: boolean;
  tier: SubscriptionTier | null;
  threatsBlockedToday: number;
  urlsCheckedToday: number;
  lastThreat: ThreatInfo | null;
  isLoggedIn: boolean;
}

export interface BackgroundMessage {
  type: MessageType;
  payload?: Record<string, unknown>;
}

export enum MessageType {
  CHECK_URL = 'check_url',
  CHECK_URL_RESPONSE = 'check_url_response',
  GET_SETTINGS = 'get_settings',
  UPDATE_SETTINGS = 'update_settings',
  REPORT_PHISHING = 'report_phishing',
  AUTH_LOGIN = 'auth_login',
  AUTH_LOGOUT = 'auth_logout',
  GET_POPUP_DATA = 'get_popup_data',
  POPUP_DATA_RESPONSE = 'popup_data_response',
  DARKWATCH_CHECK = 'darkwatch_check',
  DARKWATCH_RESPONSE = 'darkwatch_response',
  TOGGLE_PROTECTION = 'toggle_protection',
  REFRESH_TOKEN = 'refresh_token',
}
