import { config as loadDotenv } from "dotenv";
import { existsSync } from "node:fs";
import path from "node:path";
for (const candidate of [".env", "../.env", "../../.env"]) {
  const p = path.resolve(process.cwd(), candidate);
  if (existsSync(p)) {
    loadDotenv({ path: p });
    break;
  }
}

import { GeminiAamosClient, type BudgetStore } from "@app/ai-contracts";

/**
 * Live-evaluering av Gemini-lärar-tier för kylskåps-/skafferiskanning.
 *
 *   pnpm --filter @app/worker eval:scan
 *
 * Kräver AAMOS_MODE=gemini + GEMINI_API_KEY. Använder ett litet guldset av
 * publikt tillgängliga bild-URL:er (eller lokala filer). Rapporterar
 * träffbild, kvantitet, enhet, kostnad och latens. Avslutar med exitkod 1
 * om någon obligatorisk kontroll fallerar.
 */

interface ScanEvalCase {
  id: string;
  imageUrls: string[];
  locationType: "fridge" | "pantry";
  marketLocale: string;
  checks: (items: Array<{
    detectedName: string;
    brand: string | null;
    estimatedQuantity: number | null;
    unit: string | null;
    confidence: number;
    requiresConfirmation: boolean;
  }>) => { name: string; passed: boolean }[];
}

const CASES: ScanEvalCase[] = [
  {
    id: "arla-milk-fridge",
    imageUrls: ["https://upload.wikimedia.org/wikipedia/commons/6/6c/Arla_Ko_Mellanmj%C3%B6lk_1L.jpg"],
    locationType: "fridge",
    marketLocale: "sv-SE",
    checks: (items) => {
      const milk = items.find((i) =>
        /mjölk|milk/i.test(i.detectedName) || /arla/i.test(i.brand ?? ""),
      );
      return [
        { name: "hittade mjölkprodukt", passed: !!milk },
        { name: "konfidens > 0.7", passed: !!milk && milk.confidence > 0.7 },
        { name: "kräver bekräftelse om låg konfidens", passed: !!milk && (milk.confidence >= 0.92 || milk.requiresConfirmation) },
      ];
    },
  },
  {
    id: "swedish-butter-fridge",
    imageUrls: ["https://upload.wikimedia.org/wikipedia/commons/3/3e/Svenskt_Sm%C3%B6r_Normalsaltat_80-25_500g.jpg"],
    locationType: "fridge",
    marketLocale: "sv-SE",
    checks: (items) => {
      const butter = items.find((i) => /smör|butter/i.test(i.detectedName));
      return [
        { name: "hittade smör", passed: !!butter },
        { name: "konfidens > 0.7", passed: !!butter && butter.confidence > 0.7 },
      ];
    },
  },
];

class SilentBudgetStore implements BudgetStore {
  private spend = 0;
  async getDailySpendUsd(): Promise<number> {
    return this.spend;
  }
  async incrementDailySpendUsd(amountUsd: number): Promise<number> {
    this.spend += amountUsd;
    return this.spend;
  }
}

async function main() {
  const mode = process.env.AAMOS_MODE ?? "mock";
  const apiKey = process.env.GEMINI_API_KEY;

  if (mode !== "gemini" || !apiKey) {
    console.error("[eval:scan] KRÄVER AAMOS_MODE=gemini + GEMINI_API_KEY.");
    process.exit(1);
  }

  const client = new GeminiAamosClient({
    apiKey,
    model: process.env.GEMINI_MODEL,
    timeoutMs: process.env.GEMINI_TIMEOUT_MS ? Number(process.env.GEMINI_TIMEOUT_MS) : 60_000,
    dailyBudgetUsd: process.env.GEMINI_DAILY_BUDGET_USD ? Number(process.env.GEMINI_DAILY_BUDGET_USD) : 10,
    budgetStore: new SilentBudgetStore(),
  });

  console.log(`[eval:scan] Gemini-lärar-tier live-evaluering – ${CASES.length} fall\n`);

  let totalChecks = 0;
  let failedChecks = 0;
  let totalCostUsd = 0;
  let totalLatencyMs = 0;

  for (const evalCase of CASES) {
    const started = Date.now();
    let checks: { name: string; passed: boolean }[] = [];
    let error: string | null = null;

    try {
      const result = await client.runTask(
        evalCase.locationType === "fridge" ? "ANALYZE_FRIDGE_IMAGE" : "ANALYZE_PANTRY_IMAGE",
        {
          imageUrls: evalCase.imageUrls,
          locationType: evalCase.locationType,
          marketLocale: evalCase.marketLocale,
          knownItems: [],
        },
      );

      if (result.status !== "ok" || !result.output) {
        error = `status=${result.status}: ${result.error ?? "okänt fel"}`;
        checks = [{ name: "Gemini svarade ok", passed: false }];
      } else {
        const items = result.output.items;
        checks = [{ name: "Gemini svarade ok", passed: true }, ...evalCase.checks(items)];
      }

      if (result.costUsd) totalCostUsd += result.costUsd;
      if (result.latencyMs) totalLatencyMs += result.latencyMs;
    } catch (err) {
      error = (err as Error).message;
      checks = [{ name: "Gemini svarade ok", passed: false }];
    }

    const ms = Date.now() - started;
    const passed = checks.every((c) => c.passed);
    totalChecks += checks.length;
    failedChecks += checks.filter((c) => !c.passed).length;

    console.log(`${passed ? "✓" : "✗"} ${evalCase.id} (${ms} ms)`);
    for (const c of checks) if (!c.passed) console.log(`    ✗ ${c.name}`);
    if (error) console.log(`    fel: ${error}`);
  }

  console.log(`\n[eval:scan] ${CASES.length} fall, ${totalChecks} kontroller, ${failedChecks} fallerade.`);
  console.log(`[eval:scan] Total latens: ${totalLatencyMs} ms, total kostnad: ~$${totalCostUsd.toFixed(6)}`);

  if (failedChecks > 0) {
    console.log("[eval:scan] UNDERKÄND – åtgärda innan Skiva 1 går till fälttest.");
    process.exit(1);
  }
  console.log("[eval:scan] GODKÄND – fälttest kan börja.");
  process.exit(0);
}

main().catch((err) => {
  console.error("[eval:scan] KRASCH:", err);
  process.exit(1);
});
