<script>
  import { push } from 'svelte-spa-router';
  import { auth } from '../lib/stores/auth.js';
  import { fetchDeckWithQuestions, updateDeck, togglePublished, deleteDeck } from '../lib/api/decks.js';
  import { DEFAULT_DECK_CONFIG } from '../lib/deckConfig.js';
  import QuestionEditor from '../lib/QuestionEditor.svelte';

  export let params = {};
  $: deckId = params?.id;

  let title = '';
  let description = '';
  let config = { ...DEFAULT_DECK_CONFIG };
  let published = false;
  let questions = [];
  let loading = true;
  let saving = false;
  let error = null;

  $: userId = $auth.user?.id;

  let prevDeckId = '';
  $: if (deckId && userId && deckId !== prevDeckId) {
    prevDeckId = deckId;
    load();
  } else if (!deckId || !userId) {
    loading = false;
  }

  async function load() {
    if (!deckId) return;
    loading = true;
    error = null;
    try {
      const deck = await fetchDeckWithQuestions(deckId);
      if (deck.owner_id !== userId) {
        push('/');
        return;
      }
      title = deck.title ?? '';
      description = deck.description ?? '';
      config = { ...DEFAULT_DECK_CONFIG, ...(deck.config || {}) };
      published = !!deck.published;
      questions = (deck.questions || []).map((q) => ({
        prompt: q.prompt ?? '',
        explanation: q.explanation ?? '',
        answers: Array.isArray(q.answers) ? [...q.answers] : [''],
        correct_answer_indices: Array.isArray(q.correct_answer_indices) ? [...q.correct_answer_indices] : [0],
      }));
      if (questions.length === 0) {
        questions = [{ prompt: '', explanation: '', answers: [''], correct_answer_indices: [0] }];
      }
    } catch (e) {
      error = e?.message ?? 'Failed to load deck';
      questions = [{ prompt: '', explanation: '', answers: [''], correct_answer_indices: [0] }];
    } finally {
      loading = false;
    }
  }

  function addQuestion() {
    questions = [...questions, { prompt: '', explanation: '', answers: [''], correct_answer_indices: [0] }];
  }

  function removeQuestion(i) {
    questions = questions.filter((_, idx) => idx !== i);
    if (questions.length === 0) {
      questions = [{ prompt: '', explanation: '', answers: [''], correct_answer_indices: [0] }];
    }
  }

  function normalizeQuestion(q) {
    const answers = (q.answers || []).map((a) => String(a).trim()).filter(Boolean);
    if (answers.length === 0) return null;
    const indices = (q.correct_answer_indices ?? [0]).filter((i) => i >= 0 && i < answers.length);
    return {
      prompt: (q.prompt ?? '').trim(),
      explanation: (q.explanation ?? '').trim() || null,
      answers,
      correct_answer_indices: indices.length ? indices : [0],
    };
  }

  async function save() {
    const t = title.trim();
    if (!t) {
      error = 'Title is required';
      return;
    }
    if (!userId || !deckId) return;
    const normalized = questions.map(normalizeQuestion).filter(Boolean);
    if (normalized.length === 0) {
      error = 'Add at least one question with at least one answer';
      return;
    }
    saving = true;
    error = null;
    try {
      await updateDeck(deckId, {
        title: t,
        description: description.trim(),
        config,
        questions: normalized,
      });
      push('/');
    } catch (e) {
      error = e?.message ?? 'Failed to save deck';
    } finally {
      saving = false;
    }
  }

  async function togglePublish() {
    if (!deckId) return;
    try {
      await togglePublished(deckId, !published);
      published = !published;
    } catch (e) {
      error = e?.message ?? 'Failed to update publish status';
    }
  }

  async function remove() {
    if (!deckId || !confirm('Delete this deck? This cannot be undone.')) return;
    try {
      await deleteDeck(deckId);
      push('/');
    } catch (e) {
      error = e?.message ?? 'Failed to delete deck';
    }
  }

  function cancel() {
    push('/');
  }
</script>

<div class="page">
  <header class="page-header">
    <h1>Edit deck</h1>
    <div class="header-actions">
      <button type="button" class="btn btn-danger" onclick={remove} disabled={saving}>Delete</button>
      <button type="button" class="btn btn-secondary" onclick={cancel} disabled={saving}>Cancel</button>
      <button type="button" class="btn btn-primary" onclick={save} disabled={saving}>
        {saving ? 'Saving…' : 'Save'}
      </button>
    </div>
  </header>

  {#if $auth.loading}
    <p class="muted">Loading…</p>
  {:else if !userId}
    <p class="auth-required">Sign in to edit decks.</p>
  {:else if loading}
    <p class="muted">Loading…</p>
  {:else}
    <form class="deck-form" onsubmit={(e) => { e.preventDefault(); save(); }}>
      <div class="publish-row">
        <label class="toggle-label">
          <input type="checkbox" checked={published} onchange={togglePublish} />
          Published (visible in Community)
        </label>
      </div>
      <div class="form-group">
        <label for="title">Title</label>
        <input id="title" type="text" class="input" bind:value={title} placeholder="Deck title" required />
      </div>
      <div class="form-group">
        <label for="description">Description</label>
        <textarea id="description" class="input textarea" bind:value={description} placeholder="Optional description" rows="2"></textarea>
      </div>

      <h2 class="section-title">Questions</h2>
      {#each questions as question, i}
        <QuestionEditor bind:question index={i} onRemove={() => removeQuestion(i)} />
      {/each}
      <button type="button" class="btn btn-add-q" onclick={addQuestion}>+ Add question</button>

      {#if error}
        <p class="error">{error}</p>
      {/if}
    </form>
  {/if}
</div>

<style>
  .page {
    padding: 1.5rem;
    max-width: 700px;
    margin: 0 auto;
  }

  .page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 1rem;
    margin-bottom: 1.5rem;
  }

  .page-header h1 {
    margin: 0;
    font-size: 1.5rem;
    font-weight: 600;
    color: var(--text-primary, #f0f0f0);
  }

  .header-actions {
    display: flex;
    gap: 0.5rem;
  }

  .publish-row {
    margin-bottom: 1rem;
  }

  .toggle-label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.9rem;
    color: var(--text-muted, #a0a0a0);
    cursor: pointer;
  }

  .toggle-label input {
    width: 1rem;
    height: 1rem;
  }

  .auth-required,
  .muted {
    margin: 0;
    color: var(--text-muted, #a0a0a0);
  }

  .deck-form {
    display: flex;
    flex-direction: column;
  }

  .form-group {
    margin-bottom: 1rem;
  }

  .form-group label {
    display: block;
    font-size: 0.9rem;
    font-weight: 500;
    color: var(--text-muted, #a0a0a0);
    margin-bottom: 0.35rem;
  }

  .input {
    width: 100%;
    padding: 0.6rem 0.75rem;
    font-size: 0.95rem;
    border: 1px solid var(--border, #333);
    border-radius: 6px;
    background: var(--card-bg, #1e1e1e);
    color: var(--text-primary, #f0f0f0);
  }

  .textarea {
    resize: vertical;
    min-height: 2.5rem;
  }

  .section-title {
    margin: 1.5rem 0 0.75rem 0;
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--text-primary, #f0f0f0);
  }

  .btn-add-q {
    padding: 0.5rem 1rem;
    font-size: 0.9rem;
    border: 1px dashed var(--border, #333);
    border-radius: 6px;
    background: transparent;
    color: var(--text-muted, #a0a0a0);
    cursor: pointer;
    margin-bottom: 1rem;
  }

  .btn-add-q:hover {
    color: var(--text-primary, #f0f0f0);
    border-color: var(--border-hover, #444);
  }

  .error {
    margin: 0 0 1rem 0;
    font-size: 0.9rem;
    color: #ef4444;
  }

  .btn {
    padding: 0.5rem 1rem;
    font-size: 0.9rem;
    font-weight: 500;
    border: 1px solid var(--border, #333);
    border-radius: 6px;
    background: var(--card-bg, #1e1e1e);
    color: var(--text-primary, #f0f0f0);
    cursor: pointer;
  }

  .btn:hover:not(:disabled) {
    background: var(--hover-bg, #2a2a2a);
  }

  .btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
  }

  .btn-primary {
    background: var(--accent, #3b82f6);
    border-color: var(--accent, #3b82f6);
  }

  .btn-primary:hover:not(:disabled) {
    background: #2563eb;
  }

  .btn-secondary {
    background: transparent;
  }

  .btn-danger {
    background: transparent;
    color: #ef4444;
    border-color: #ef4444;
  }

  .btn-danger:hover:not(:disabled) {
    background: rgba(239, 68, 68, 0.15);
  }
</style>
