'use strict';
/* ──────────────────────────────────────────────────────────────────
   Exam Preparation Module — API wrappers
   Thin LS.api wrappers under window.EP.api.*
   ────────────────────────────────────────────────────────────────── */

(function () {
  const base = (examKey) => `/api/exam-prep/${encodeURIComponent(examKey)}`;

  const api = {
    /* Track registry / metadata */
    listTracks: ()              => LS.api('/api/exam-prep/tracks'),
    getInfo:    (examKey)       => LS.api(`${base(examKey)}/info`),

    /* Future endpoints (F2-F10) — placeholders so calling code can be written
       against the final shape. Wire them up as routes ship. */
    listVariants: (examKey)               => LS.api(`${base(examKey)}/variants`),
    getVariant:   (examKey, n)            => LS.api(`${base(examKey)}/variants/${n}/tasks`),
    listTopics:   (examKey)               => LS.api(`${base(examKey)}/topics`),
    getTopicTasks:(examKey, slug, query)  => LS.api(`${base(examKey)}/topics/${encodeURIComponent(slug)}/tasks${qs(query)}`),
    getPracticeNext: (examKey, query)     => LS.api(`${base(examKey)}/practice/next${qs(query)}`),
    getDashboard: (examKey)               => LS.api(`${base(examKey)}/dashboard`),
    getPlan:      (examKey)               => LS.api(`${base(examKey)}/plan`),
    savePlan:     (examKey, body)         => LS.api(`${base(examKey)}/plan`, { method: 'PUT', body }),
    deletePlan:   (examKey)               => LS.api(`${base(examKey)}/plan`, { method: 'DELETE' }),
    saveAttempt:  (body)                  => LS.api(`/api/exam-prep/attempts`, { method: 'POST', body }),
    startMock:    (examKey, body)         => LS.api(`${base(examKey)}/mock/start`, { method: 'POST', body }),
    mockAnswer:   (mockId, body)          => LS.api(`/api/exam-prep/mock/${mockId}/answer`, { method: 'POST', body }),
    mockFinish:   (mockId)                => LS.api(`/api/exam-prep/mock/${mockId}/finish`, { method: 'POST' }),
    mockResult:   (mockId)                => LS.api(`/api/exam-prep/mock/${mockId}/result`),
  };

  function qs(obj) {
    if (!obj || typeof obj !== 'object') return '';
    const parts = Object.entries(obj)
      .filter(([, v]) => v !== undefined && v !== null && v !== '')
      .map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`);
    return parts.length ? `?${parts.join('&')}` : '';
  }

  window.EP = window.EP || {};
  window.EP.api = api;
})();
