import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import path from "node:path";
import { captureTrainingSample } from "../lib/shadow-capture.js";

/**
 * Evaluering av shadow-capture – verifierar att träningspar sparas korrekt.
 * Använder en lokal fixture-bild (data-URL) så att inget nätverk krävs.
 *
 *   S3_MODE=mock pnpm --filter @app/worker eval:capture
 */

const __filename = fileURLToPath(import.meta.url);
const FIXTURES = path.resolve(path.dirname(__filename), "fixtures");

const fridgeBytes = readFileSync(path.join(FIXTURES, "fridge-1.jpg"));
const fridgeDataUrl = `data:image/jpeg;base64,${fridgeBytes.toString("base64")}`;

async function main() {
  const result = await captureTrainingSample({
    taskType: "ANALYZE_FRIDGE_IMAGE",
    inputS3Keys: ["eval/fridge-1.jpg"],
    output: {
      items: [
        {
          detectedName: "Testmjölk",
          canonicalIngredientId: null,
          brand: "Testa",
          estimatedQuantity: 1,
          unit: "LITER",
          bestBeforeDate: null,
          confidence: 0.97,
          requiresConfirmation: false,
          boundingBox: null,
        },
      ],
      imageQualityIssues: [],
    },
    modelVersion: "gemini-3.1-flash-lite",
    promptVersion: "gemini-fridge-v1",
    latencyMs: 1234,
    costUsd: 0.0001,
    inputTokens: 400,
    outputTokens: 120,
    consentFlags: {
      personalization: true,
      anonymizedImprovement: true,
      imageTraining: true,
    },
    readUrl: async (key: string) => {
      if (key === "eval/fridge-1.jpg") return fridgeDataUrl;
      return `http://localhost/v1/mock-s3/${encodeURIComponent(key)}?sig=dummy`;
    },
  });

  if (!result.ok) {
    console.error("[eval:capture] misslyckades:", result.error);
    process.exit(1);
  }
  console.log(`[eval:capture] träningspar sparat på nyckel: ${result.key}`);
}

main().catch((err) => {
  console.error(err);
  process.exit(1);
});
