/* global React, supabase */
const { useState, useEffect, useRef, useCallback } = React;

const _SB_URL = 'https://bhbksroyflcnqvauttvg.supabase.co';
const _SB_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImJoYmtzcm95ZmxjbnF2YXV0dHZnIiwicm9sZSI6ImFub24iLCJpYXQiOjE3Nzc4OTc1MTMsImV4cCI6MjA5MzQ3MzUxM30.Zb0zSuNcDtu5XlYsUevxz4PXVZGsL-bAE9wXwrkLR0k';

// Default data — overridden by Supabase (or localStorage cache) if present
const DEFAULT_HERO_IMAGES = [
  { url: "images/carousel/01.jpg",  caption: "One Haus Developments — South Florida" },
  { url: "images/carousel/02.jpeg", caption: "One Haus Developments — South Florida" },
  { url: "images/carousel/03.jpeg", caption: "One Haus Developments — South Florida" },
  { url: "images/carousel/04.jpg",  caption: "One Haus Developments — South Florida" },
  { url: "images/carousel/05.png",  caption: "One Haus Developments — South Florida" },
  { url: "images/carousel/06.jpg",  caption: "One Haus Developments — South Florida" },
];

const DEFAULT_SERVICES = [
  {
    num: "01",
    title: "Ground Up Construction",
    body: "Custom new builds from ground-up, managed end-to-end. Civil engineering rigor, architect-led design, and a single point of accountability from permits to final inspection.",
    meta: ["Custom homes", "Ground-up builds", "Turnkey delivery"],
    image: "images/services/ground-up-construction.jpg",
  },
  {
    num: "02",
    title: "High-End Renovations",
    body: "Full-scale remodels and reimagined existing properties. From structural reconfigurations to fixture-level specification — we handle every decision and every subcontractor.",
    meta: ["Full home remodels", "Kitchens & baths", "Historic updates"],
    image: "images/services/renovations.png",
  },
  {
    num: "03",
    title: "Real Estate Investment",
    body: "Sourcing off-market deals with exceptional upside. Hands-off for investors, with no management fees and no hidden costs — just transparent returns and a single point of contact.",
    meta: ["Off-market sourcing", "No management fees", "Transparent returns"],
    image: "images/services/investment.png",
  },
];

const DEFAULT_PROJECTS = [
  {
    id: "orra-bay",
    name: "Orra Bay",
    loc: "Bay Harbor Islands, FL",
    cat: "Renovation",
    size: "pt-xl",
    year: "2024",
    sqft: "TBD",
    beds: "TBD",
    kind: "Luxury Condo Renovation",
    cover: "images/orra-bay/cover.jpg",
    blurb: "A full gut renovation of a luxury waterfront condo in Bay Harbor Islands. Complete redesign of all living spaces with high-end finishes, custom millwork, and panoramic bay views throughout.",
    hasBeforeAfter: true,
    before: "images/orra-bay/before-01.png",
    after: "images/orra-bay/cover.jpg",
    gallery: [
      "images/orra-bay/cover.jpg",
      "images/orra-bay/02.jpg",
      "images/orra-bay/03.jpg",
      "images/orra-bay/04.jpg",
      "images/orra-bay/05.jpg",
      "images/orra-bay/06.jpg",
      "images/orra-bay/07.jpg",
      "images/orra-bay/08.jpg",
    ],
  },
  {
    id: "baystone",
    name: "Baystone",
    loc: "South Florida",
    cat: "Investment",
    size: "pt-md",
    year: "2024",
    sqft: "TBD",
    beds: "TBD",
    kind: "Investment Renovation",
    cover: "images/baystone/cover.jpg",
    blurb: "Off-market investment property sourced, fully renovated, and repositioned for resale. Complete interior overhaul delivering high-spec finishes and a modern open-plan layout.",
    hasBeforeAfter: true,
    before: "images/baystone/before-01.png",
    after: "images/baystone/cover.jpg",
    gallery: [
      "images/baystone/cover.jpg",
      "images/baystone/02.jpg",
      "images/baystone/03.jpg",
      "images/baystone/04.jpg",
    ],
  },
  {
    id: "azure-palms",
    name: "Azure Palms",
    loc: "South Florida",
    cat: "Renovation",
    size: "pt-lg",
    year: "2024",
    sqft: "TBD",
    beds: "TBD",
    kind: "Full Home Remodel",
    cover: "images/azure-palms/cover.jpg",
    blurb: "High-end kitchen and living space transformation. Dual-island chef's kitchen with marble flooring, custom wood cabinetry, and designer lighting — executed start to finish by our in-haus team.",
    gallery: [
      "images/azure-palms/cover.jpg",
      "images/azure-palms/02.jpg",
      "images/azure-palms/03.jpg",
      "images/azure-palms/04.jpg",
      "images/azure-palms/05.jpg",
      "images/azure-palms/06.jpeg",
    ],
  },
  {
    id: "casa-mare",
    name: "Casa Mare",
    loc: "South Florida",
    cat: "Renovation",
    size: "pt-sm",
    year: "2023",
    sqft: "TBD",
    beds: "TBD",
    kind: "Luxury Renovation",
    cover: "images/casa-mare/cover.jpg",
    blurb: "Luxury residential renovation with a dramatic material palette. Custom fireplace feature wall, travertine and marble throughout, and architect-designed spatial reconfiguration.",
    hasBeforeAfter: true,
    before: "images/casa-mare/before-01.png",
    after: "images/casa-mare/cover.jpg",
    gallery: [
      "images/casa-mare/cover.jpg",
      "images/casa-mare/02.jpg",
      "images/casa-mare/03.jpg",
      "images/casa-mare/04.jpg",
      "images/casa-mare/05.jpg",
    ],
  },
  {
    id: "casa-palms",
    name: "Casa Palms",
    loc: "South Florida",
    cat: "In-Progress",
    size: "pt-wd",
    year: "2025",
    sqft: "TBD",
    beds: "TBD",
    kind: "In-Progress · Rendering",
    cover: "images/casa-palms/cover.jpg",
    blurb: "Currently in design development. A full renovation of a Mediterranean-style residence — preserving original architectural character while transforming all interior spaces.",
    gallery: [
      "images/casa-palms/cover.jpg",
      "images/casa-palms/02.jpg",
      "images/casa-palms/03.jpeg",
    ],
  },
  {
    id: "solmar",
    name: "Solmar",
    loc: "South Florida",
    cat: "In-Progress",
    size: "pt-md",
    year: "2025",
    sqft: "TBD",
    beds: "TBD",
    kind: "In-Progress · Rendering",
    cover: "images/solmar/cover.png",
    blurb: "Currently in construction. High-end kitchen and bathroom renovation with custom Italian cabinetry, book-matched marble tile, and freestanding soaking tub.",
    gallery: [
      "images/solmar/cover.png",
      "images/solmar/02.png",
    ],
  },
  {
    id: "luma-bay",
    name: "Luma Bay",
    loc: "South Florida",
    cat: "In-Progress",
    size: "pt-md",
    year: "2025",
    sqft: "TBD",
    beds: "TBD",
    kind: "In-Progress · Rendering",
    cover: "images/luma-bay/cover.png",
    blurb: "In development. A waterfront residential project featuring contemporary architecture with clean sightlines, custom millwork, and seamless indoor-outdoor living.",
    gallery: ["images/luma-bay/cover.png"],
  },
  {
    id: "casa-verde",
    name: "Casa Verde",
    loc: "South Florida",
    cat: "In-Progress",
    size: "pt-md",
    year: "2025",
    sqft: "TBD",
    beds: "TBD",
    kind: "In-Progress · Active",
    cover: "images/casa-verde/cover.jpeg",
    blurb: "Active renovation currently underway. Full interior transformation of a South Florida single-family home — structural updates, finishes, and systems top to bottom.",
    gallery: [
      "images/casa-verde/cover.jpeg",
      "images/casa-verde/02.jpeg",
    ],
  },
];

const DEFAULT_TESTIMONIALS = [
  {
    quote: "We had an amazing experience working with One Haus for our home renovation. From start to finish, their team was professional, detail-oriented, and truly committed to bringing our vision to life. Communication was clear, timelines were respected, and the quality of workmanship exceeded our expectations.",
    who: "Tara G",
    role: "Homeowner",
    avatar: "TG",
  },
  {
    quote: "Such a professional service all around! John and the One Haus team went above and beyond for their job with me. They were always available to answer questions and wouldn't settle unless we had a perfect result. I couldn't be happier!",
    who: "Justin S",
    role: "Client",
    avatar: "JS",
  },
  {
    quote: "Choosing One Haus was the best decision. Working with them was seamless, easy, and my home is so beautiful. Everyone that walks in gives me so many compliments! Thank you to the One Haus team!",
    who: "Noa E",
    role: "Homeowner",
    avatar: "NE",
  },
  {
    quote: "Great contact, renovations are made in a very professional way, great finishing. Thank you Mr. Wahnoun for renovating our apartment according to my vision and dream.",
    who: "Marlene A",
    role: "Client",
    avatar: "MA",
  },
  {
    quote: "Professional, quick to reply and they get the job done. Only good things to say about One Haus.",
    who: "Jordan G",
    role: "Client",
    avatar: "JG",
  },
];

const DEFAULT_TEAM = [
  {
    name: "Jonathan Wahnoun",
    role: "President & Co-Founder",
    portrait: "images/team/jonathan.jpg",
    bio: [
      "Jonathan Wahnoun is a seasoned construction professional with over 15 years of experience in Florida's residential building industry. Holding a Florida General Contractor license, he brings a robust background in civil engineering to his work, ensuring structural integrity and design excellence in every project.",
      "Throughout his career, Jonathan has led a wide range of projects, including custom home builds, full-scale renovations, and complex remodeling efforts. His expertise spans all phases of construction — from initial planning and permitting to final inspections — making him a trusted partner for homeowners and developers alike.",
    ],
  },
  {
    name: "Danielle Elfassy",
    role: "Vice President & Co-Founder",
    portrait: "images/team/danielle.jpg",
    bio: [
      "Danielle Elfassy is a highly driven real estate professional with a strong background in residential real estate and deep expertise in the South Florida market. As a licensed real estate agent and Vice President at One Haus Developments, she is instrumental in driving strategic growth and overseeing the firm's real estate initiatives.",
      "Danielle specializes in sourcing high-value off-market opportunities, leveraging her extensive network and market insight to identify properties with exceptional potential. Her hands-on approach and market knowledge make her a trusted partner for investors, buyers, and developers.",
    ],
  },
];

const DEFAULT_TEAM_FUTURE = [
  { name: "In-Haus Architect", role: "Design" },
  { name: "Interior Designer", role: "Interiors" },
  { name: "Project Manager", role: "Delivery" },
  { name: "Licensed Realtor", role: "Sourcing" },
];

const DEFAULT_CONTACT = {
  phone: "(786) 877-0965",
  email: "danielle@onehausdev.com",
  emailDanielle: "Danielle@onehausdev.com",
  emailJonathan: "Jonathan@onehausdev.com",
  area: "Miami · Fort Lauderdale · Palm Beach",
  hours: "Mon–Fri · 9:00–6:00 ET",
};

const DEFAULT_COPY = {
  heroEyebrow: "One Haus Developments · South Florida",
  heroH1Main: "Build smart. Renovate bold. Invest wisely.",
  heroH1Italic: "All in one place",
  heroH1Suffix: "— all in Haus.",
  companyLede: "One Haus Developments is a full-service real estate development company specializing in residential construction, high-end renovations, and strategic real estate investments across South Florida.",
  companyQuote: "From breaking ground on new builds to reimagining existing properties, we bring a detail-driven approach and deep local-market knowledge to every project.",
  companyBody1: "Our team is passionate about transforming spaces and uncovering value — whether it's designing custom homes, executing full-scale remodels, or sourcing off-market deals with exceptional upside. We manage every phase so our clients can invest, build, or renovate with confidence and ease.",
  companyBody2: "With over 15 years of experience and a reputation for quality, transparency, and results, One Haus is your trusted partner for creating and maximizing residential real estate in Miami, Fort Lauderdale, and beyond.",
  projectsLede: "A cross-section of full renovations, investor-sourced properties, and projects currently underway. Tap any project to view the full gallery.",
  servicesLede: "We operate an in-haus team of a licensed realtor, general contractor, architect, interior designer, and project manager — so you never have to juggle vendors.",
  teamLede: "Founded by a general contractor and a real-estate strategist — two complementary disciplines that anchor every decision we make for a client.",
  serviceAreaLede: "We build and renovate across South Florida — with deep network roots in Miami-Dade and Broward, and active projects moving north into Palm Beach.",
  contactBody: "Miami, Fort Lauderdale, and broader South Florida. One team, one point of contact, one accountable answer from day one.",
  contactHomeownerSub: "Project scope, timeline, and anything else on your mind. We'll respond within two business days with next steps.",
  contactInvestorSub: "Share a bit about your investment profile — we'll reach back within two business days with current off-market deals.",
  footerCopyright: "© 2026 One Haus Developments · South Florida",
  instagramUrl: "https://www.instagram.com/onehausdev/",
};

// Load admin overrides from localStorage (set by admin portal)
function loadAdminData() {
  try {
    const stored = localStorage.getItem("onehaus_admin_data");
    if (stored) return JSON.parse(stored);
  } catch (e) {}
  return null;
}

const _admin = loadAdminData();

const HERO_IMAGES  = (_admin && _admin.heroImages)   ? _admin.heroImages   : DEFAULT_HERO_IMAGES;
const SERVICES     = (_admin && _admin.services)    ? _admin.services    : DEFAULT_SERVICES;
const PROJECTS     = (_admin && _admin.projects)     ? _admin.projects     : DEFAULT_PROJECTS;
const TESTIMONIALS = (_admin && _admin.testimonials) ? _admin.testimonials : DEFAULT_TESTIMONIALS;
const TEAM         = (_admin && _admin.team)         ? _admin.team         : DEFAULT_TEAM;
const TEAM_FUTURE  = DEFAULT_TEAM_FUTURE;
const CONTACT      = (_admin && _admin.contact)      ? _admin.contact      : DEFAULT_CONTACT;
const COPY         = Object.assign({}, DEFAULT_COPY, (_admin && _admin.copy) ? _admin.copy : {});

Object.assign(window, {
  HERO_IMAGES, SERVICES, PROJECTS, TESTIMONIALS, TEAM, TEAM_FUTURE, CONTACT, COPY,
  DEFAULT_HERO_IMAGES, DEFAULT_SERVICES, DEFAULT_PROJECTS, DEFAULT_TESTIMONIALS, DEFAULT_TEAM, DEFAULT_CONTACT, DEFAULT_COPY,
});

(async function _syncFromSupabase() {
  try {
    if (typeof supabase === 'undefined') return;
    const sb = supabase.createClient(_SB_URL, _SB_KEY);
    const { data, error } = await sb
      .from('onehaus_content')
      .select('data')
      .eq('key', 'content')
      .maybeSingle();
    if (error || !data) return;
    const c = data.data;
    if (c.heroImages)   window.HERO_IMAGES   = c.heroImages;
    if (c.services)     window.SERVICES      = c.services;
    if (c.projects)     window.PROJECTS      = c.projects;
    if (c.testimonials) window.TESTIMONIALS  = c.testimonials;
    if (c.team)         window.TEAM          = c.team;
    if (c.contact)      window.CONTACT       = c.contact;
    if (c.copy)         window.COPY          = Object.assign({}, window.DEFAULT_COPY, c.copy);
    localStorage.setItem('onehaus_admin_data', JSON.stringify(c));
    if (typeof window.__onDataRefresh === 'function') window.__onDataRefresh();
  } catch(e) {}
})();
