/* =====================================================================
   gallery.js — filterbare Mediengalerie + Lightbox (für medien.html)

   MEDIA-Array unten mit echten Bildern befüllen sobald verfügbar
   (assets/img/...). Bis dahin rendert jeder Eintrag ohne "src" einen
   Platzhalter-Block mit passendem Alt-Text, damit die Struktur schon
   steht und nur noch Bilder eingehängt werden müssen.
   ===================================================================== */

const MEDIA = [
  { id: 1, category: "dogfather", alt: "Dogfather Profilbild", src: "assets/img/dogfather-portrait.jpg" },
  { id: 2, category: "dogfather", alt: "Dogfather — Team Dogi Karte", src: "assets/img/card-dogfather.jpg" },
  { id: 3, category: "team", alt: "VanVan — Team Dogi Karte", src: "assets/img/card-vanvan.jpg" },
  { id: 4, category: "team", alt: "Diene — Team Dogi Karte", src: "assets/img/card-diene.jpg" },
  { id: 5, category: "team", alt: "Miss — Team Dogi Karte", src: "assets/img/card-miss.jpg" },
  { id: 6, category: "team", alt: "Marina alias FlauschiRex — Team Dogi Karte", src: "assets/img/card-marina.jpg" },
  { id: 7, category: "hasidog", alt: "HasiDog &amp; Casper Sticker", src: "assets/img/sticker-hasidog-casper.png" },
  { id: 8, category: "hasidog", alt: "Husky Casper", src: "assets/img/casper-1.jpg" },
  { id: 9, category: "hasidog", alt: "Casper &amp; Dogfather", src: "assets/img/dogfather-casper-1.jpg" },
  { id: 10, category: "hasidog", alt: "Casper", src: "assets/img/casper-3.jpg" },
  { id: 11, category: "hasidog", alt: "Casper", src: "assets/img/casper-4.jpg" },
  { id: 12, category: "hasidog", alt: "Dogfather &amp; Casper Nase an Nase", src: "assets/img/dogfather-casper-2.jpg" },
  { id: 13, category: "hasidog", alt: "Schlaft schön — Buch-Illustration", src: "assets/img/hasidog-casper-book.jpg" },
  { id: 14, category: "hasidog", alt: "Casper unterwegs", src: "assets/img/casper-2.jpg" },
  { id: 15, category: "manager", alt: "Dogfather Logo", src: "assets/img/logo-dogfather.png" },
  // TODO: Aktionen-Fotos (Weihnachtsaktion, VanVan-Teddys) ergänzen, sobald vorhanden.
];

function renderGalleryItem(item) {
  const media = item.src
    ? `<img src="${item.src}" alt="${item.alt}" loading="lazy" />`
    : `<div class="img-placeholder">${item.alt}<br><span class="small">Foto folgt</span></div>`;
  return `
    <figure class="gallery-item card" data-category="${item.category}" data-index="${item.id}">
      ${media}
      <figcaption class="small muted" style="margin-top:.6rem;">${item.alt}</figcaption>
    </figure>`;
}

function initGallery() {
  const grid = document.getElementById("gallery-grid");
  const filterBar = document.getElementById("gallery-filters");
  if (!grid || !filterBar) return;

  const categories = ["alle", ...new Set(MEDIA.map((m) => m.category))];
  filterBar.innerHTML = categories
    .map(
      (c, i) =>
        `<button class="filter-btn${i === 0 ? " active" : ""}" data-filter="${c}">${c[0].toUpperCase() + c.slice(1)}</button>`
    )
    .join("");

  function draw(filter) {
    const items = filter === "alle" ? MEDIA : MEDIA.filter((m) => m.category === filter);
    grid.innerHTML = items.map(renderGalleryItem).join("") || `<p class="muted">Keine Bilder in dieser Kategorie.</p>`;
    bindLightbox();
  }

  filterBar.addEventListener("click", (e) => {
    const btn = e.target.closest(".filter-btn");
    if (!btn) return;
    filterBar.querySelectorAll(".filter-btn").forEach((b) => b.classList.remove("active"));
    btn.classList.add("active");
    draw(btn.dataset.filter);
  });

  function bindLightbox() {
    const lb = document.getElementById("lightbox");
    const lbContent = document.getElementById("lightbox-content");
    if (!lb || !lbContent) return;
    grid.querySelectorAll(".gallery-item").forEach((el) => {
      el.addEventListener("click", () => {
        lbContent.innerHTML = el.innerHTML;
        lb.classList.add("open");
      });
    });
  }

  draw("alle");

  const lb = document.getElementById("lightbox");
  if (lb) {
    lb.addEventListener("click", (e) => {
      if (e.target === lb || e.target.closest(".lightbox-close")) {
        lb.classList.remove("open");
      }
    });
    document.addEventListener("keydown", (e) => {
      if (e.key === "Escape") lb.classList.remove("open");
    });
  }
}

document.addEventListener("DOMContentLoaded", initGallery);
