// Book cover component + catalogue data.
// Renders a stylised gradient "book cover" with serif italic title + author.

const PALETTES = [
  { bg: "linear-gradient(160deg, #1a3a8a 0%, #0e2160 60%, #061439 100%)", accent: "#ffd166" },
  { bg: "linear-gradient(160deg, #b85c3a 0%, #7d2f17 100%)", accent: "#f4e3c2" },
  { bg: "linear-gradient(160deg, #0f5e4f 0%, #08372c 100%)", accent: "#e4d3a8" },
  { bg: "linear-gradient(160deg, #d4a574 0%, #8f6a3a 100%)", accent: "#3a2a14" },
  { bg: "linear-gradient(160deg, #5a4e8c 0%, #2d245a 100%)", accent: "#ffd1e8" },
  { bg: "linear-gradient(160deg, #c44d4d 0%, #7a1f1f 100%)", accent: "#f4d6c0" },
  { bg: "linear-gradient(160deg, #2c4a6b 0%, #14253b 100%)", accent: "#f0c987" },
  { bg: "linear-gradient(160deg, #6b8e23 0%, #3a4f15 100%)", accent: "#f5e8c0" },
  { bg: "linear-gradient(160deg, #8a3a5c 0%, #4a1a32 100%)", accent: "#f4d6e3" },
  { bg: "linear-gradient(160deg, #d6a82e 0%, #8a6817 100%)", accent: "#2a1f0a" },
  { bg: "linear-gradient(160deg, #1f6f8b 0%, #0d3a4a 100%)", accent: "#f0e6d2" },
  { bg: "linear-gradient(160deg, #4a3a8a 0%, #25205a 100%)", accent: "#f4d6c8" },
  { bg: "linear-gradient(160deg, #b87333 0%, #6e4419 100%)", accent: "#f5e3c4" },
  { bg: "linear-gradient(160deg, #2e6e3a 0%, #154220 100%)", accent: "#e6d4a8" },
  { bg: "linear-gradient(160deg, #1c1c1c 0%, #0a0a0a 100%)", accent: "#d8b878" },
];

const ORNAMENTS = ["✦", "❋", "✺", "❖", "✧", "❀", "⁂", "❉"];

function BookCover({ book, idx = 0, compact = false }) {
  if (book.cover) {
    return (
      <div className="cover" style={{ padding: 0 }}>
        <img
          src={book.cover}
          alt={`${book.title} — ${book.author}`}
          style={{ position: "absolute", inset: 0, width: "100%", height: "100%", objectFit: "cover", zIndex: 1 }}
        />
      </div>
    );
  }
  const palette = PALETTES[idx % PALETTES.length];
  const orn = ORNAMENTS[idx % ORNAMENTS.length];
  return (
    <div className="cover" style={{ background: palette.bg }}>
      <div className="meta" style={{ display: "flex", justifyContent: "space-between", alignItems: "start", color: palette.accent }}>
        <span className="pub">{book.imprint || "House of Books"}</span>
        <span className="ornament" style={{ borderColor: palette.accent + "80", color: palette.accent }}>{orn}</span>
      </div>
      <div className="body">
        <div className="title" style={{ color: palette.accent }}>{book.title}</div>
        <div className="author">{book.author}</div>
      </div>
    </div>
  );
}

// Hero arc — real book covers (files live in /uploads).
const HERO_BOOKS = [
  { title: "A Patra Aripă", author: "Rebecca Yarros", cover: "/uploads/a patra aripa.png" },
  { title: "Alchemised", author: "SenLinYu", cover: "/uploads/alchemised.jpeg" },
  { title: "Toate Cărțile", author: "Igor Bergler", cover: "/uploads/testamentul_lui_abraham_toate_cartile.jpg" },
  { title: "Katabasis", author: "R. F. Kuang", cover: "/uploads/katabasis.png" },
  { title: "Văduva de Fier", author: "Xiran Jay Zhao", cover: "/uploads/vaduva de fier.jpeg" },
];

// 15 mixed catalogue books (Romanian + translated classics + contemporary)
const BOOKS = [
  { title: "Maitreyi", author: "Mircea Eliade", price: "45 lei", category: "Roman", imprint: "Clasici" },
  { title: "Ion", author: "Liviu Rebreanu", price: "52 lei", category: "Roman", imprint: "Patrimoniu" },
  { title: "Solenoid", author: "Mircea Cărtărescu", price: "89 lei", category: "Contemporan", imprint: "Humanitas", badge: "Nou" },
  { title: "Maestrul și Margareta", author: "Mihail Bulgakov", price: "68 lei", category: "Roman", imprint: "Polirom" },
  { title: "Numele Trandafirului", author: "Umberto Eco", price: "75 lei", category: "Roman", imprint: "Polirom", badge: "Favorit" },
  { title: "Moromeții", author: "Marin Preda", price: "59 lei", category: "Roman", imprint: "Patrimoniu" },
  { title: "1984", author: "George Orwell", price: "42 lei", category: "Distopie", imprint: "Polirom" },
  { title: "Un veac de singurătate", author: "Gabriel García Márquez", price: "65 lei", category: "Roman", imprint: "RAO" },
  { title: "Crimă și pedeapsă", author: "F. M. Dostoievski", price: "72 lei", category: "Clasic", imprint: "Polirom" },
  { title: "Mândrie și prejudecată", author: "Jane Austen", price: "48 lei", category: "Romantic", imprint: "Litera" },
  { title: "Baltagul", author: "Mihail Sadoveanu", price: "38 lei", category: "Clasic", imprint: "Patrimoniu" },
  { title: "Doamna Bovary", author: "Gustave Flaubert", price: "55 lei", category: "Clasic", imprint: "Polirom" },
  { title: "Micul Prinț", author: "Antoine de Saint-Exupéry", price: "29 lei", category: "Pentru toți", imprint: "Rao", badge: "Cadou" },
  { title: "Pe drum", author: "Jack Kerouac", price: "52 lei", category: "Cult", imprint: "Polirom" },
  { title: "Enigma Otiliei", author: "George Călinescu", price: "49 lei", category: "Clasic", imprint: "Patrimoniu" },
];

// Seed shown until/unless the database returns rows (fallback for first load).
const FEATURED_SEED = [{
  title: "Solenoid", author: "Mircea Cărtărescu", price: "89 lei", imprint: "Humanitas", badge: "Nou",
  description: "Un roman-fluviu care te ia pe sus și nu îți mai dă drumul. Pentru cei care iubesc literatura fără jumătăți de măsură — un labirint de gânduri, vise și amintiri, scris ca o melodie lentă.",
}];
const CATALOGUE_SEED = BOOKS;

// Reads books for a section from Supabase; falls back to seed data on empty/error.
function useBooks(section, seed) {
  const [books, setBooks] = React.useState(seed);
  React.useEffect(() => {
    let active = true;
    if (!window.sb) return;
    window.sb
      .from("books")
      .select("*")
      .eq("section", section)
      .order("position", { ascending: true })
      .then(({ data, error }) => {
        if (!active || error || !data || !data.length) return;
        setBooks(data.map((b) => ({ ...b, cover: b.cover_url || null })));
      });
    return () => { active = false; };
  }, [section]);
  return books;
}

Object.assign(window, { BookCover, BOOKS, HERO_BOOKS, PALETTES, FEATURED_SEED, CATALOGUE_SEED, useBooks });
