/* ── SCROLL TO SECTION ── */
function scrollToSection(id) {
  const el = document.getElementById(id);
  if (el) window.scrollTo({ top: el.getBoundingClientRect().top + window.pageYOffset - 80, behavior: 'smooth' });
}
window.scrollToSection = scrollToSection;

/* ── NAV SCROLL SHADOW ── */
function initNav() {
  const nav = document.getElementById('mainNav');
  if (nav) {
    window.addEventListener('scroll', () => nav.classList.toggle('scrolled', window.scrollY > 20));
  }

  const hamburger = document.getElementById('hamburger');
  const mobileMenu = document.getElementById('mobileMenu');
  if (hamburger && mobileMenu) {
    hamburger.addEventListener('click', () => {
      const open = mobileMenu.classList.toggle('open');
      hamburger.innerHTML = open ? '✕' : '☰';
    });
    mobileMenu.querySelectorAll('a').forEach(a => a.addEventListener('click', () => {
      mobileMenu.classList.remove('open');
      hamburger.innerHTML = '☰';
    }));
  }
}

/* ── HERO SLIDESHOW ── */
let curSlide = 0;
let slideTimer;
const DURATION = 4000;

function goToSlide(idx) {
  const slides = document.querySelectorAll('.slide');
  const sdots = document.querySelectorAll('.sdot');
  const progFill = document.getElementById('slideProgressFill');

  if (!slides.length) return;

  slides[curSlide].classList.remove('active');
  if (sdots[curSlide]) sdots[curSlide].classList.remove('active');

  curSlide = (idx + slides.length) % slides.length;

  slides[curSlide].classList.add('active');
  if (sdots[curSlide]) sdots[curSlide].classList.add('active');

  if (progFill) {
    progFill.style.transition = 'none';
    progFill.style.width = '0%';
    setTimeout(() => {
      progFill.style.transition = `width ${DURATION}ms linear`;
      progFill.style.width = '100%';
    }, 30);
  }

  clearInterval(slideTimer);
  slideTimer = setInterval(() => goToSlide(curSlide + 1), DURATION);
}
window.goToSlide = goToSlide;

/* ── TESTIMONIALS ── */
const TESTIMONIALS = [
  { name: "James Wambui", role: "Graduate · Healthcare", color: "#C8521A", text: "The institute is very professional and organized. I acquired more than I anticipated. Your dedication, passion, and love to train are so amazing. Continue with the good and amazing work. I highly recommend the institution to other learners." },
  { name: "Cecilia Mwangi", role: "Graduate · Social Work", color: "#3B82F6", text: "The best place to be, to grow, and nurture your career. Thank you for the experience." },
  { name: "Judith Dutira", role: "Student · Caregiving", color: "#8B5CF6", text: "Big up GoCare. A place everyone would wish to be trained. The trainers are exceptional and genuinely care about your success." },
  { name: "Maureen Wafula", role: "Graduate · CNA", color: "#10B981", text: "I had first thought of training in another institution. But after completing the course at GoCare and now working, I value the knowledge and skills I acquired. I am forever grateful. Long live GoCare!" },
  { name: "Joseph Karani", role: "Graduate · Theatre Tech", color: "#D4A843", text: "The best decision I did was to train with GoCare. Best professional study, equipment, and guaranteed attachment. Go! Go! GoCare!" },
  { name: "Nancy Njoroge", role: "Graduate · Community Health", color: "#7C2D3E", text: "The school fees are very affordable and fair with installment payment options. Compared to the quality training offered, I confirm value for money and will recommend the college any time." }
];
let curT = 0;

function buildAvatars() {
  const row = document.getElementById('avatarRow');
  if (!row) return;
  row.innerHTML = '';
  TESTIMONIALS.forEach((t, i) => {
    const d = document.createElement('div');
    d.className = 'avatar-circle' + (i === curT ? ' active' : '');
    d.onclick = () => setTestimonial(i);
    d.innerHTML = `<div class="avatar-circle-placeholder" style="background:${t.color};">${t.name[0]}</div>`;
    row.appendChild(d);
  });
}
function setTestimonial(idx) {
  curT = (idx + TESTIMONIALS.length) % TESTIMONIALS.length;
  const t = TESTIMONIALS[curT];
  const nameEl = document.getElementById('tName');
  const textEl = document.getElementById('tText');
  const roleEl = document.getElementById('tRole');
  if (!nameEl || !textEl || !roleEl) return;

  nameEl.style.opacity = '0'; textEl.style.opacity = '0';
  setTimeout(() => {
    nameEl.textContent = t.name;
    roleEl.textContent = t.role;
    textEl.textContent = t.text;
    nameEl.style.transition = textEl.style.transition = 'opacity 0.4s';
    nameEl.style.opacity = textEl.style.opacity = '1';
  }, 180);
  buildAvatars();
}
function changeTestimonial(dir) { setTestimonial(curT + dir); }
window.changeTestimonial = changeTestimonial;

const ALL_COURSES = [
  { title: "Diploma in Mortuary Science", cat: "Healthcare", img: "pics/Diploma in Mortuary Science .jpeg", grad: "linear-gradient(135deg,#F7941D,#582D81)" },
  { title: "Certificate in Home-Based Care Support (HCA)", cat: "Healthcare", img: "pics/Certificate in Home-Based Care Support.jpeg", grad: "linear-gradient(135deg,#582D81,#F7941D)" },
  { title: "Certified Nursing Assistant (CNA)", cat: "Nursing", img: "pics/Certified Nursing Assistant.jpeg", grad: "linear-gradient(135deg,#F7941D,#582D81)" },
  { title: "Healthcare Support Services / CNA – Level 5", cat: "Nursing", img: "pics/Healthcare Support Services CNA – Level 5.jpeg", grad: "linear-gradient(135deg,#582D81,#F7941D)" },
  { title: "Certificate in Caregiving – Level 4 (Advanced HCA)", cat: "Caregiving", img: "pics/Certificate in Caregiving – Level 4.jpeg", grad: "linear-gradient(135deg,#F7941D,#582D81)" },
  { title: "Caregiver Course / CNA – Level II", cat: "Caregiving", img: "pics/Caregiver Course CNA – Level II.jpeg", grad: "linear-gradient(135deg,#582D81,#1a1a1a)" },
  { title: "Health Care Assistant / Caregiver Course", cat: "Healthcare", img: "pics/Health Care Assistant Caregiver Course.jpeg", grad: "linear-gradient(135deg,#F7941D,#582D81)" },
  { title: "Diploma in Perioperative Theatre Technology", cat: "Theatre Tech", img: "pics/Diploma in Perioperative Theatre Technology.jpeg", grad: "linear-gradient(135deg,#582D81,#F7941D)" },
  { title: "Certificate in Perioperative Theatre Technology", cat: "Theatre Tech", img: "pics/Certificate in Perioperative Theatre Technology.jpeg", grad: "linear-gradient(135deg,#F7941D,#582D81)" },
  { title: "Diploma in Orthopaedic and Trauma Medicine", cat: "Medical", img: "pics/Diploma in Orthopaedic and Trauma Medicine.jpeg", grad: "linear-gradient(135deg,#582D81,#F7941D)" },
  { title: "Certificate in Orthopaedic and Trauma Medicine", cat: "Medical", img: "pics/Certificate in Orthopaedic and Trauma Medicine.jpeg", grad: "linear-gradient(135deg,#F7941D,#582D81)" },
  { title: "Certificate in Mortuary Science", cat: "Healthcare", img: "pics/Certificate in Mortuary Science.jpeg", grad: "linear-gradient(135deg,#582D81,#1a1a1a)" },
  { title: "Skill Development Council (SDC) Canada Courses", cat: "International", img: "pics/Skill Development Council (SDC) Canada Courses.jpeg", grad: "linear-gradient(135deg,#F7941D,#582D81)" },
  { title: "American Medical Certification Association (AMCA)", cat: "International", img: "pics/American Medical Certification Association.jpeg", grad: "linear-gradient(135deg,#582D81,#F7941D)" },
  { title: "Diploma in Community Health Assistant", cat: "Community", img: "pics/Diploma in Community Health Assistant.jpeg", grad: "linear-gradient(135deg,#F7941D,#582D81)" },
  { title: "Certificate in Community Health Assistant", cat: "Community", img: "pics/Certificate in Community Health Assistant.jpeg", grad: "linear-gradient(135deg,#582D81,#F7941D)" },
  { title: "Diploma in Social Work and Community Development", cat: "Social Work", img: "pics/Diploma in Social Work and Community Development.jpeg", grad: "linear-gradient(135deg,#F7941D,#582D81)" },
  { title: "Certificate in Social Work and Community Development", cat: "Social Work", img: "pics/Certificate in Social Work and Community Development.jpeg", grad: "linear-gradient(135deg,#582D81,#1a1a1a)" },
  { title: "Certificate in Front Office Operations", cat: "Hospitality", img: "pics/Certificate in Front Office Operations.jpeg", grad: "linear-gradient(135deg,#F7941D,#582D81)" },
  { title: "Certificate in Food & Beverage Production (Culinary Arts)", cat: "Hospitality", img: "pics/Certificate in Food & Beverage Production.jpeg", grad: "linear-gradient(135deg,#582D81,#F7941D)" },
  { title: "Certificate in Housekeeping and Accommodation", cat: "Hospitality", img: "pics/Certificate in Housekeeping and Accommodation.jpeg", grad: "linear-gradient(135deg,#F7941D,#582D81)" },
  { title: "Certificate in Office Administration", cat: "Business", img: "pics/Certificate in Office Administration.jpeg", grad: "linear-gradient(135deg,#582D81,#F7941D)" },
  { title: "Certificate in Office Assistant", cat: "Business", img: "pics/Certificate in Office Assistant.jpeg", grad: "linear-gradient(135deg,#F7941D,#582D81)" },
  { title: "Diploma in ICT Technician", cat: "ICT", img: "pics/Diploma in ICT Technician.jpeg", grad: "linear-gradient(135deg,#582D81,#1a1a1a)" },
  { title: "International Certificate of Digital Literacy (ICDL)", cat: "ICT", img: "pics/International Certificate of Digital Literacy.jpeg", grad: "linear-gradient(135deg,#F7941D,#582D81)" },
];

const PER_PAGE = 6;
let curPage = 0;
const TOTAL_PAGES = Math.ceil(ALL_COURSES.length / PER_PAGE);

function renderCourses() {
  const grid = document.getElementById('coursesGrid');
  if (!grid) return;
  const start = curPage * PER_PAGE;
  grid.innerHTML = ALL_COURSES.slice(start, start + PER_PAGE).map(c => `
    <div class="course-card" data-tilt>
      <div class="course-thumb">
        <div class="course-thumb-overlay"></div>
        <img src="${encodeURI(c.img)}" alt="${c.title}">
      </div>
      <div class="course-body">
        <div class="course-category">${c.cat}</div>
        <div class="course-title">${c.title}</div>
      </div>
    </div>`).join('');

  // Staggered Entrance
  grid.querySelectorAll('.course-card').forEach((card, i) => {
    card.style.opacity = '0';
    card.style.transform = 'translateY(24px)';
    setTimeout(() => {
      card.style.transition = 'opacity 0.6s cubic-bezier(0.4, 0, 0.2, 1), transform 0.6s cubic-bezier(0.4, 0, 0.2, 1)';
      card.style.opacity = '1';
      card.style.transform = 'translateY(0)';
    }, i * 80);
  });

  initTiltEffect();

  const pg = document.getElementById('pagination');
  if (!pg) return;
  let h = `<button class="page-btn" ${curPage === 0 ? 'disabled' : ''} onclick="goToPage(${curPage - 1})">←</button>`;
  for (let i = 0; i < TOTAL_PAGES; i++)
    h += `<button class="page-btn ${i === curPage ? 'active' : ''}" onclick="goToPage(${i})">${i + 1}</button>`;
  h += `<button class="page-btn" ${curPage === TOTAL_PAGES - 1 ? 'disabled' : ''} onclick="goToPage(${curPage + 1})">→</button>`;
  pg.innerHTML = h;
}

function initTiltEffect() {
  const cards = document.querySelectorAll('.course-card');

  cards.forEach(card => {
    card.addEventListener('mousemove', e => {
      const rect = card.getBoundingClientRect();
      const x = e.clientX - rect.left;
      const y = e.clientY - rect.top;

      const centerX = rect.width / 2;
      const centerY = rect.height / 2;

      const rotateX = (centerY - y) / 10;
      const rotateY = (x - centerX) / 10;

      card.style.transform = `perspective(1000px) rotateX(${rotateX}deg) rotateY(${rotateY}deg) translateY(-8px)`;
    });

    card.addEventListener('mouseleave', () => {
      card.style.transform = 'perspective(1000px) rotateX(0deg) rotateY(0deg) translateY(0)';
    });
  });
}

function goToPage(idx) {
  curPage = Math.max(0, Math.min(TOTAL_PAGES - 1, idx));
  renderCourses();
  scrollToSection('coursesSection');
}
window.goToPage = goToPage;

/* ── PARTNERS ── */
const ALL_PARTNERS = [
  { name: "TVETA", img: "partner/TVETA.png" },
  { name: "KNEC", img: "partner/knec.jpg" },
  { name: "NITA", img: "partner/national_industrial_training_authority_logo.jpg" },
  { name: "ICDL", img: "partner/ICDL.png" },
  { name: "AMCA", img: "partner/amca.png" },
  { name: "Ministry of Education", img: "partner/ministry of education.jpg" },
  { name: "SDCC Canada", img: "partner/SDCC.png" },
  { name: "KATTI", img: "partner/KATTI.png" },
  { name: "AMCA Exams", img: "partner/amca exams.png" },
  { name: "TVETA Curriculum", img: "partner/tveta curriculum.png" }
];

function renderPartners() {
  const grid = document.getElementById('partnersGrid');
  if (!grid) return;
  grid.innerHTML = ALL_PARTNERS.map(p => `
    <div class="partner-logo">
      <img src="${encodeURI(p.img)}" alt="${p.name}" style="max-width:100%; max-height:60px; object-fit:contain;">
    </div>`).join('');
}

/* ── NEWSLETTER ── */
function handleSubscribe() {
  const input = document.getElementById('newsletterEmail');
  const form = document.getElementById('newsletterForm');
  if (input && input.value.includes('@') && input.value.includes('.')) {
    form.innerHTML = '<div style="color:white;font-size:16px;font-weight:600;padding:20px 0;">✅ Thank you! You\'re now subscribed.</div>';
  } else if (input) {
    input.style.outline = '2px solid #EF4444';
    input.placeholder = 'Please enter a valid email';
    setTimeout(() => { input.style.outline = ''; input.placeholder = 'Enter your email address'; }, 2500);
  }
}
window.handleSubscribe = handleSubscribe;

/* ── SCROLL REVEAL ── */
function initReveal() {
  const revealObs = new IntersectionObserver(entries => {
    entries.forEach(e => { if (e.isIntersecting) { e.target.classList.add('visible'); revealObs.unobserve(e.target); } });
  }, { threshold: 0.1 });
  document.querySelectorAll('.reveal').forEach(el => revealObs.observe(el));
}

// Export init for loader
window.initScripts = () => {
  initNav();
  initReveal();
  renderCourses();
  renderPartners();
  buildAvatars();
  goToSlide(0);
};
// Initialize everything when DOM is ready
document.addEventListener('DOMContentLoaded', () => {
  if (window.initScripts) {
    window.initScripts();
  }
});
