import { mkdir, writeFile } from "fs/promises";
import { dirname, join } from "path";
import { fileURLToPath } from "url";

const __dirname = dirname(fileURLToPath(import.meta.url));
const root = join(__dirname, "..");
const base = "https://grvsummit.com/wp-content/uploads";

const assets = [
  { url: `${base}/2024/10/Great-Rift-Valley-Innovation-Summit-03-1-1.png`, dest: "public/branding/logo.png" },
  { url: `${base}/2024/10/Great-Rift-Valley-Innovation-Summit-03-1-1-100x100.png`, dest: "public/branding/logo-icon.png" },
  { url: `${base}/2024/10/image-1.jpg`, dest: "public/branding/logo-wordmark.jpg" },
  { url: `${base}/2024/11/Web-Asset_Exibition-Booth-Mockup-1.png`, dest: "public/branding/booth-mockup.png" },
  { url: `${base}/2025/02/Sarma_editeed-removebg-preview.png`, dest: "public/branding/speakers/sarma.png" },
  { url: `${base}/2025/02/Solomon_edited-removebg-preview.png`, dest: "public/branding/speakers/solomon.png" },
  { url: `${base}/2025/02/Sunil_edited-removebg-preview.png`, dest: "public/branding/speakers/sunil.png" },
  { url: `${base}/2025/02/Tewabech_edited-removebg-preview.png`, dest: "public/branding/speakers/tewabech.png" },
  { url: `${base}/2025/02/Beamlak_edited-removebg-preview-1.png`, dest: "public/branding/speakers/beamlak.png" },
  { url: `${base}/2025/02/Abrahame_Endrias_edited-removebg-preview.png`, dest: "public/branding/speakers/abrhame.png" },
  { url: `${base}/2025/02/Mekdim_edteid-removebg-preview.png`, dest: "public/branding/speakers/mekdim.png" },
  { url: `${base}/2025/02/brook_taye_edited-removebg-preview.png`, dest: "public/branding/speakers/brook.png" },
  { url: `${base}/2025/02/yared_edited-removebg-preview.png`, dest: "public/branding/speakers/yared.png" },
  { url: `${base}/2025/02/Biruh_edited-removebg-preview.png`, dest: "public/branding/speakers/biruh.png" },
  { url: `${base}/2025/02/Abraham_edited-removebg-preview.png`, dest: "public/branding/speakers/abraham.png" },
  { url: `${base}/2025/02/Amity_edited-removebg-preview.png`, dest: "public/branding/speakers/amity.png" },
  { url: `${base}/2025/02/Tigist_edited-removebg-preview.png`, dest: "public/branding/speakers/tigist.png" },
  { url: `${base}/2025/02/Adam_edited-removebg-preview.png`, dest: "public/branding/speakers/adam.png" },
  { url: `${base}/2025/02/lulite_edited-removebg-preview.png`, dest: "public/branding/speakers/lulite.png" },
  { url: `${base}/2025/02/dagmawit_edited-removebg-preview.png`, dest: "public/branding/speakers/dagmawit.png" },
  { url: `${base}/2025/02/samiya_edited-removebg-preview.png`, dest: "public/branding/speakers/samiya.png" },
  {
    url: "https://www.google.com/s2/favicons?domain=lifelineaddis.com&sz=128",
    dest: "public/branding/winners/lifeline-addis.png",
  },
  {
    url: "https://www.google.com/s2/favicons?domain=globedock.et&sz=128",
    dest: "public/branding/winners/globedock-academy.png",
  },
  {
    url: "https://www.google.com/s2/favicons?domain=muyalogy.com&sz=128",
    dest: "public/branding/winners/muyalogy.png",
  },
];

async function download(url, dest) {
  const path = join(root, dest);
  await mkdir(dirname(path), { recursive: true });
  const res = await fetch(url);
  if (!res.ok) throw new Error(`Failed ${url}: ${res.status}`);
  const buf = Buffer.from(await res.arrayBuffer());
  await writeFile(path, buf);
  console.log(`✓ ${dest}`);
}

for (const { url, dest } of assets) {
  try {
    await download(url, dest);
  } catch (e) {
    console.warn(`✗ ${dest}:`, e.message);
  }
}
