// ──────────────────────────────────────────────────────────────────────────
// Creator-Score & automatische Analyse — reine Funktionen (client-sicher).
// Bewertet nicht nur nach Größe, sondern nach Wachstum, Aktivität, Engagement,
// Zusammenarbeit und Community-Fit.
// ──────────────────────────────────────────────────────────────────────────
import type { CreatorStatusKey } from "./constants";

export type ScoreParts = {
  growth: number; // 0–100 Teilscore
  activity: number;
  engagement: number;
  potential: number;
};

export type ScoreInput = {
  growthPct30: number | null; // Wachstum letzte 30 Tage in %
  daysSinceActivity: number | null; // Tage seit letzter Social-Aktivität
  rating: number | null; // 1–5 manuelle Einschätzung (Potenzial/Zusammenarbeit)
  engagement: number | null; // 0–100, falls Engagement-Kennzahl vorhanden
};

const clamp = (n: number, lo = 0, hi = 100) => Math.max(lo, Math.min(hi, n));

/** Liefert Teilscores 0–100. */
export function scoreParts(i: ScoreInput): ScoreParts {
  // Wachstum: 0 % → 40, +30 % → 100, negativ zieht runter.
  const growth = i.growthPct30 == null ? 45 : clamp(50 + i.growthPct30 * 1.8);
  // Aktivität: heute aktiv → 100, ab 30 Tagen → 0.
  const activity =
    i.daysSinceActivity == null ? 55 : clamp(100 - (i.daysSinceActivity / 30) * 100);
  // Engagement: direkte Kennzahl, sonst neutral.
  const engagement = i.engagement == null ? 50 : clamp(i.engagement);
  // Potenzial: manuelle 1–5 Bewertung.
  const potential = i.rating == null ? 50 : clamp((i.rating / 5) * 100);
  return { growth, activity, engagement, potential };
}

/** Gewichteter Gesamtscore 0–100. */
export function creatorScore(i: ScoreInput): number {
  const p = scoreParts(i);
  const score = p.growth * 0.35 + p.activity * 0.3 + p.engagement * 0.15 + p.potential * 0.2;
  return Math.round(clamp(score));
}

export function scoreTone(score: number): "good" | "mid" | "low" {
  if (score >= 70) return "good";
  if (score >= 45) return "mid";
  return "low";
}

// ── Automatische Hinweise ──────────────────────────────────────────────────

export type Insight = {
  kind: "growth" | "warning" | "inactivity" | "recommendation";
  tone: "good" | "bad" | "neutral";
  text: string;
};

export type InsightInput = {
  growthPct7: number | null;
  growthPct30: number | null;
  daysSinceActivity: number | null;
  category: string | null;
  categoryLabel: string | null;
  status: CreatorStatusKey;
  score: number;
};

/** Interpretiert die Daten und erzeugt lesbare Hinweise (deutsch). */
export function creatorInsights(name: string, i: InsightInput): Insight[] {
  const out: Insight[] = [];

  if (i.growthPct30 != null && i.growthPct30 >= 15) {
    out.push({ kind: "growth", tone: "good", text: `${name} ist in den letzten 30 Tagen um ${Math.round(i.growthPct30)}% gewachsen.` });
  } else if (i.growthPct7 != null && i.growthPct7 >= 10) {
    out.push({ kind: "growth", tone: "good", text: `Starker Wochenzuwachs: +${Math.round(i.growthPct7)}% in 7 Tagen.` });
  }

  if (i.growthPct30 != null && i.growthPct30 <= -10) {
    out.push({ kind: "warning", tone: "bad", text: `Reichweite ist in 30 Tagen um ${Math.abs(Math.round(i.growthPct30))}% gesunken.` });
  }

  if (i.daysSinceActivity != null) {
    if (i.daysSinceActivity >= 21) out.push({ kind: "inactivity", tone: "bad", text: `Creator war ${i.daysSinceActivity} Tage nicht aktiv.` });
    else if (i.daysSinceActivity >= 10) out.push({ kind: "inactivity", tone: "neutral", text: `Aktivität nachlassend — ${i.daysSinceActivity} Tage kein neuer Content.` });
  }

  const growing = (i.growthPct30 ?? 0) >= 12;
  if (growing && i.categoryLabel && (i.status === "OBSERVING" || i.status === "CONTACTING" || i.status === "CONVERSATION")) {
    out.push({ kind: "recommendation", tone: "good", text: `Passt aufgrund von Wachstum und Kategorie „${i.categoryLabel}" sehr gut ins Avaria-Netzwerk.` });
  }

  return out;
}
