/*
  HOME — hero with search + continue-watching row + trending rows.
*/
(function () {
  'use strict';

  // quick-search chips in the hero — edit freely
  const QUICK_SEARCHES = ['Action', 'Comedy', 'Sci-Fi', 'Drama', 'Horror', 'Animation', 'Crime'];

  SR.app.initChrome();

  const sections = document.getElementById('sections');

  const form = document.getElementById('heroForm');
  const input = document.getElementById('heroInput');
  if (form && input) {
    form.addEventListener('submit', (e) => {
      e.preventDefault();
      const q = input.value.trim();
      location.href = q ? `search.html?q=${encodeURIComponent(q)}` : 'search.html';
    });
  }

  const chips = document.getElementById('heroChips');
  if (chips) {
    for (const term of QUICK_SEARCHES) {
      const a = document.createElement('a');
      a.className = 'chip';
      a.href = `search.html?q=${encodeURIComponent(term)}`;
      a.textContent = term;
      chips.appendChild(a);
    }
  }

  const cont = SR.cards.continueRow();
  if (cont) sections.appendChild(cont);

  function appendRow(data, title) {
    if (!data || !data.count) return;
    sections.appendChild(SR.cards.row(title, data.results.map(SR.cards.normalizeItem)));
  }

  (async () => {
    try {
      appendRow(await SR.api.trending('movie', 'day'), 'Trending movies today');
    } catch (e) {
      SR.app.showError(e, 'could not load trending movies');
    }
    try {
      appendRow(await SR.api.trending('tv', 'week'), 'Trending shows this week');
    } catch (e) {
      SR.app.showError(e, 'could not load trending shows');
    }
  })();
})();
