<script>
  /** @type {{ id: string, title: string } | null} */
  export let deck = null;
  /** @type {number} 1-5 */
  export let initialRating = 0;
  /** @type {string} */
  export let initialComment = '';
  export let onSubmit = () => {};
  export let onClose = () => {};

  let selectedStars = 0;
  let comment = '';

  $: if (deck) {
    selectedStars = initialRating || 0;
    comment = initialComment ?? '';
  }

  function setStars(n) {
    selectedStars = n;
  }

  function handleSubmit() {
    if (selectedStars < 1) return;
    onSubmit({ rating: selectedStars, comment: comment.trim() || null });
    onClose();
  }

  function handleBackdropClick(e) {
    if (e.target === e.currentTarget) onClose();
  }
</script>

{#if deck}
  <!-- svelte-ignore a11y_no_static_element_interactions -->
  <div
    class="modal-backdrop"
    role="dialog"
    aria-modal="true"
    aria-labelledby="rating-modal-title"
    tabindex="-1"
    onclick={handleBackdropClick}
    onkeydown={(e) => e.key === 'Escape' && onClose()}
  >
    <!-- svelte-ignore a11y_no_static_element_interactions -->
    <div
      class="modal"
      role="document"
      tabindex="-1"
      onclick={(e) => e.stopPropagation()}
      onkeydown={(e) => e.key === 'Escape' && onClose()}
    >
      <h2 id="rating-modal-title" class="modal-title">Rate: {deck.title}</h2>
      <div class="stars-row">
        {#each [1, 2, 3, 4, 5] as n}
          <button
            type="button"
            class="star-btn"
            aria-label="{n} star{n === 1 ? '' : 's'}"
            onclick={() => setStars(n)}
          >
            {n <= selectedStars ? '★' : '☆'}
          </button>
        {/each}
      </div>
      <label class="comment-label" for="rating-comment">Comment (optional)</label>
      <textarea
        id="rating-comment"
        class="comment-input"
        placeholder="Add a comment…"
        bind:value={comment}
        rows="3"
      ></textarea>
      <div class="modal-actions">
        <button type="button" class="btn btn-ghost" onclick={onClose}>Cancel</button>
        <button type="button" class="btn btn-primary" onclick={handleSubmit} disabled={selectedStars < 1}>
          Submit
        </button>
      </div>
    </div>
  </div>
{/if}

<style>
  .modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 1rem;
  }

  .modal {
    background: var(--card-bg, #1a1a1a);
    border: 1px solid var(--border, #333);
    border-radius: 12px;
    padding: 1.5rem;
    max-width: 400px;
    width: 100%;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
  }

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

  .stars-row {
    display: flex;
    gap: 0.25rem;
    margin-bottom: 1rem;
  }

  .star-btn {
    background: none;
    border: none;
    padding: 0.25rem;
    cursor: pointer;
    font-size: 1.75rem;
    line-height: 1;
    color: #eab308;
    transition: transform 0.15s;
  }

  .star-btn:hover {
    transform: scale(1.15);
  }

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

  .comment-input {
    width: 100%;
    padding: 0.5rem 0.75rem;
    font-size: 0.95rem;
    font-family: inherit;
    color: var(--text-primary, #f0f0f0);
    background: var(--input-bg, #252525);
    border: 1px solid var(--border, #333);
    border-radius: 8px;
    resize: vertical;
    margin-bottom: 1.25rem;
    box-sizing: border-box;
  }

  .comment-input::placeholder {
    color: var(--text-muted, #666);
  }

  .modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 0.5rem;
  }

  .btn {
    padding: 0.5rem 1rem;
    font-size: 0.9rem;
    font-weight: 500;
    border-radius: 8px;
    cursor: pointer;
  }

  .btn-ghost {
    background: transparent;
    border: 1px solid var(--border, #333);
    color: var(--text-muted, #a0a0a0);
  }

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

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

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

  .btn-primary:disabled {
    opacity: 0.5;
    cursor: not-allowed;
  }
</style>
