export interface AdminDashboardData {
  timestamp: string;
  status: "healthy" | "unhealthy";
  system: {
    memory: Record<string, unknown>;
    disk: {
      used_percent?: number;
      total_gb?: number;
      free_gb?: number;
    };
  };
  providers: Record<string, ProviderStatus>;
  cleanup: {
    files_cleaned: number;
    tracked_files_count: number;
  };
  rate_limits: {
    active_clients: number;
  };
  config: {
    max_file_size_mb: number;
    supported_extensions: string[];
    translation_service: string;
  };
}

export interface ProviderStatus {
  name: string;
  available: boolean;
  last_check: string | null;
  latency_ms?: number;
  error?: string;
  /** true when availability is derived from configuration (no live call) */
  config_only?: boolean;
  /** optional display label provided by the backend */
  label?: string;
}

export interface CleanupResponse {
  status: "success" | "error";
  files_cleaned: number;
  message: string;
}

export type StatsPeriod = "today" | "week" | "month";

export interface ProviderBreakdownItem {
  count: number;
  percentage: number;
}

export interface FormatBreakdownItem {
  count: number;
  percentage: number;
}

export interface TopUser {
  user_id: string;
  email: string;
  translation_count: number;
}

export interface TranslationStatsData {
  period: StatsPeriod;
  total_translations: number;
  total_translations_last_period: number;
  error_rate: number;
  error_count: number;
  success_count: number;
  top_users: TopUser[];
  provider_breakdown: Record<string, ProviderBreakdownItem>;
  format_breakdown: Record<string, FormatBreakdownItem>;
}

export interface TranslationStatsResponse {
  data: TranslationStatsData;
  meta: {
    generated_at: string;
  };
}

/* ── Modèles & abonnements (paliers IA) ─────────────────────── */

/** Route d'un palier : un modèle via un canal donné (multi-canaux). */
export interface AiTierRoute {
  provider: string; // openrouter | openrouter_premium | deepseek | zhipu | minimax | openai | zai
  model: string;
  label?: string;
}

export interface AiTierSettings {
  /** Ancien format : modèles OpenRouter (l'ordre est la priorité de secours). */
  models: string[];
  /** Modèle par défaut ; vide = premier de la liste = défaut du plan. */
  default_model: string;
  /** Routes multi-canaux {provider, model} — priment sur models/default_model. */
  routes: AiTierRoute[];
}

export interface AiTiersSettings {
  essential: AiTierSettings;
  premium: AiTierSettings;
}

/* ── Statistiques enrichies (revenus, MRR, liste d'attente) ── */

export interface AdminStatsResponse {
  users: {
    total: number;
    active_this_month: number;
    by_plan: Record<string, number>;
  };
  translations: {
    docs_this_month: number;
    pages_this_month: number;
  };
  revenue: {
    collected_total: number;
    collected_30d: number;
    credits_purchased: number;
    payments_30d: number;
    currency: string;
  };
  mrr_estimated: {
    total: number;
    by_plan: Record<string, number>;
  };
  ai_tier_usage: {
    essential: number;
    premium: number;
    classic: number;
    other: number;
  };
  waitlist_count: number;
  cache: Record<string, unknown>;
  config: Record<string, unknown>;
}

/* ── Marketing (relances email) ─────────────────────────────── */

export interface MarketingAudiences {
  audiences: Record<string, number>;
  unsubscribed_count: number;
}

export interface MarketingAudiencesResponse {
  data: MarketingAudiences;
  meta: Record<string, unknown>;
}

export interface MarketingEmailHistoryEntry {
  id: string;
  sent_at: string;
  completed_at?: string;
  audience: string;
  subject: string;
  test_mode: boolean;
  test_recipient?: string;
  recipients_total: number;
  sent_count: number;
  failed_count: number;
  failed: { email: string; error: string }[];
  content_hash?: string;
}

export interface MarketingEmailSendResponse {
  data: {
    test_mode: boolean;
    sent?: number;
    failed?: number;
    recipient?: string;
    queued?: number;
    audience?: string;
  };
  meta: Record<string, unknown>;
}

/* ── Marketing : génération IA + réductions ─────────────────── */

export interface MarketingEmailGenerateRequest {
  provider: string;
  model?: string;
  brief: string;
  language?: string;
  tone?: string;
  promo_code?: string;
}

export interface MarketingEmailGenerateResponse {
  data: {
    subject: string;
    html: string;
    provider: string;
    model: string;
    language: string;
  };
  meta: Record<string, unknown>;
}

/** Code promo (réduction) — miroir de data/promo_codes.json côté serveur. */
export interface PromoCode {
  code: string;
  active: boolean;
  type: "percent" | "amount";
  value: number;
  plans: string[];
  max_redemptions: number | null;
  times_used: number;
  expires_at: string | null;
  stripe_coupon_id: string | null;
  stripe_promotion_code_id: string | null;
  stripe_mode: "test" | "live" | "local_only";
  created_at: string;
  deactivated_at: string | null;
  last_used_at?: string;
}

export interface PromoListResponse {
  data: { promos: PromoCode[] };
  meta: Record<string, unknown>;
}
