// Exercise 2: pick the right fetch caching strategy
//
// Given three scenarios, choose the correct `fetch` options so each
// route gets the rendering behavior described. Replace each `???`
// with the correct `RequestInit`-like options object.
//
// Scenario A: a marketing page listing blog posts that change rarely —
//             should be pre-rendered at build time and reused for every
//             visitor (static rendering).
//
// Scenario B: a stock ticker widget that must show a fresh price on
//             every single request — never served stale.
//
// Scenario C: a "trending articles" list that can be up to 5 minutes
//             (300 seconds) stale — should regenerate in the background
//             after that window (ISR).

type FetchOptions = { cache?: "force-cache" | "no-store"; next?: { revalidate?: number } };

async function getBlogPosts(): Promise<unknown> {
  const options: FetchOptions = {} as any; // TODO: Scenario A — replace with the correct options
  return fetch("https://api.example.com/posts", options).then((r) => r.json());
}

async function getStockPrice(): Promise<unknown> {
  const options: FetchOptions = {} as any; // TODO: Scenario B — replace with the correct options
  return fetch("https://api.example.com/stock/ACME", options).then((r) => r.json());
}

async function getTrendingArticles(): Promise<unknown> {
  const options: FetchOptions = {} as any; // TODO: Scenario C — replace with the correct options
  return fetch("https://api.example.com/trending", options).then((r) => r.json());
}

// --- self-check: do not modify below this line ---
function describeOptions(fn: () => Promise<unknown>, source: string) {
  return source;
}

const checks = [
  { name: "getBlogPosts", src: getBlogPosts.toString(), expect: /cache:\s*["']force-cache["']/ },
  { name: "getStockPrice", src: getStockPrice.toString(), expect: /cache:\s*["']no-store["']/ },
  { name: "getTrendingArticles", src: getTrendingArticles.toString(), expect: /revalidate:\s*300/ },
];

let allPass = true;
for (const c of checks) {
  const pass = c.expect.test(c.src);
  if (!pass) allPass = false;
  console.log(`${c.name}: ${pass ? "PASS" : "FAIL"}`);
}
console.log(allPass ? "ALL PASS" : "SOME FAILED");
