// 이선생 TKTX — 메인 페이지 로직
// Supabase 연결 + DEMO 모드 fallback (localStorage)
// 모듈화: store / icons / floating / notices / products / qna / reviews / inquiry / urgent / external

import { createStore } from './store.js';
import { renderFloating } from './modules/floating.js';
import { renderHero } from './modules/hero.js';
import { renderMente } from './modules/mente.js';
import { renderNotices } from './modules/notices.js';
import { renderProducts } from './modules/products.js';
import { renderQnA } from './modules/qna.js';
import { renderReviews, bindReviewForm } from './modules/reviews.js';
import { renderContactChannels, renderInquiryIntro } from './modules/inquiry.js';
import { renderMainChannel } from './modules/main-channel.js';
import { renderUrgentNotice } from './modules/urgent.js';
import { renderExternalLinks } from './modules/external.js';
import { renderFooter } from './modules/footer.js';
import { initModal } from './modules/modal.js';
// (main-channel은 위에서 이미 import됨 — 라인 17)

(async function () {
  const cfg = window.__TKTX_CONFIG__;
  const store = createStore(cfg);

  // 모달 시스템 먼저 부팅
  initModal();

  // 모든 콘텐츠 병렬 로드
  const [cms, notices, products, qna, reviews, externalLinks] = await Promise.all([
    store.getCmsConfig(),
    store.list('notices'),
    store.list('products'),
    store.list('qna'),
    store.list('reviews'),
    store.list('external_links')
  ]);

  // 렌더
  renderHero(cms);
  renderMente(cms);
  renderFooter(cms);
  renderFloating(cms);
  renderContactChannels(cms);
  renderInquiryIntro(cms);
  renderMainChannel(cms);
  renderNotices(notices);
  renderProducts(products);
  renderQnA(qna);
  renderReviews(reviews);
  renderExternalLinks(externalLinks);
  renderUrgentNotice(cms);

  // 폼 바인딩
  bindReviewForm(store, cfg);

  // 햄버거 메뉴 바인딩
  bindMobileDrawer();
  bindBackToTop();

  // 글로벌 store 노출 (admin이 동일 도메인일 경우 디버깅용)
  window.__store = store;
})();

function bindBackToTop() {
  const btn = document.getElementById('back-to-top');
  if (!btn) return;
  window.addEventListener('scroll', () => {
    btn.classList.toggle('is-visible', window.scrollY > 500);
  }, { passive: true });
  btn.addEventListener('click', () => {
    window.scrollTo({ top: 0, behavior: 'smooth' });
  });
}

function bindMobileDrawer() {
  const toggle = document.getElementById('nav-toggle');
  const drawer = document.getElementById('mobile-drawer');
  const closeBtn = document.getElementById('drawer-close');
  if (!toggle || !drawer) return;

  const open = () => {
    drawer.classList.add('is-open');
    toggle.classList.add('is-open');
    document.body.style.overflow = 'hidden';
  };
  const close = () => {
    drawer.classList.remove('is-open');
    toggle.classList.remove('is-open');
    document.body.style.overflow = '';
  };
  toggle.addEventListener('click', () => {
    drawer.classList.contains('is-open') ? close() : open();
  });
  closeBtn?.addEventListener('click', close);
  drawer.querySelectorAll('a').forEach(a => a.addEventListener('click', close));
}
