// Site data + helpers (Holibags)
//
// Product catalogue and settings (opening hours, class types) now live on the
// server. The frontend fetches them at boot via /api/products and /api/settings.
// Cart contents are still kept in localStorage so a refresh doesn't lose them.

const CATEGORIES = ["All", "Home", "Bags", "Gifts", "Accessories"];

const REVIEWS = [
  {
    name: "Ferne Fairfull",
    badge: "Local Guide",
    rating: 5,
    quote:
      "Booked for a lesson with Heather, she was absolutely fantastic. I went from never using a sewing machine to confidently making a drawstring bag. Heather has a lovely way of teaching",
  },
  {
    name: "Catherine Lamont",
    badge: null,
    rating: 5,
    quote:
      "Very happy with all repairs. I wouldn't go anywhere else! Very professional service.",
  },
];

const BUSINESS = {
  name: "Holibags",
  tagline: "Handmade pieces & sewing classes — Kirkcaldy.",
  address: "305a High Street, Kirkcaldy, KY1 1JL",
  phone: "01592 000 000",
  email: "hello@holibags.co.uk",
  socials: [
    { label: "Facebook", href: "https://www.facebook.com/p/Topstitch-Kirkcaldy-61553635991331/" },
  ],
};

// Default hours/classes used until /api/settings resolves
const DEFAULT_HOURS = [
  { day: "Mon", hours: "10:00 – 17:00" },
  { day: "Tue", hours: "10:00 – 17:00" },
  { day: "Wed", hours: "10:00 – 17:00" },
  { day: "Thu", hours: "10:00 – 19:00" },
  { day: "Fri", hours: "10:00 – 17:00" },
  { day: "Sat", hours: "10:00 – 16:00" },
  { day: "Sun", hours: "Closed" },
];

const DEFAULT_CLASSES = [
  { id: "intro", name: "Beginner Intro", duration: "2 hours", price: 35, desc: "Never used a sewing machine? We'll start from zero — threading, tension, your first seam." },
  { id: "machine", name: "Machine Basics", duration: "2 hours", price: 40, desc: "Get confident on your own machine. Bring it along — we'll demystify every dial and foot." },
  { id: "project", name: "Project-Based", duration: "3 hours", price: 55, desc: "Pick a project (drawstring bag, cushion, simple skirt) and finish it in a single session." },
  { id: "intermediate", name: "Intermediate", duration: "3 hours", price: 60, desc: "Zips, linings, fitted garments. For sewists ready to take on more ambitious makes." },
];

// ---- Cart (was "enquiry") persistence ---------------------------------------
const CART_KEY = "holibags.cart.v1";
function loadCart() {
  try {
    const raw = localStorage.getItem(CART_KEY);
    if (raw) return JSON.parse(raw);
  } catch (e) {}
  return [];
}
function saveCart(items) {
  try { localStorage.setItem(CART_KEY, JSON.stringify(items)); } catch (e) {}
}

// ---- API helpers ------------------------------------------------------------
async function apiFetchProducts() {
  try {
    const res = await fetch("/api/products");
    if (!res.ok) throw new Error(res.statusText);
    return await res.json();
  } catch (e) {
    console.error("[api] /api/products failed", e);
    return [];
  }
}

async function apiFetchSettings() {
  try {
    const res = await fetch("/api/settings");
    if (!res.ok) throw new Error(res.statusText);
    return await res.json();
  } catch (e) {
    console.error("[api] /api/settings failed", e);
    return { hours: DEFAULT_HOURS, classes: DEFAULT_CLASSES };
  }
}

// Return the first image for a product (back-compat with older single-image rows).
function productPrimaryImage(p) {
  if (Array.isArray(p.images) && p.images.length) return p.images[0];
  if (p.image) return p.image;
  return "";
}

Object.assign(window, {
  CATEGORIES, REVIEWS, BUSINESS,
  DEFAULT_HOURS, DEFAULT_CLASSES,
  CART_KEY, loadCart, saveCart,
  apiFetchProducts, apiFetchSettings, productPrimaryImage,
});
