<script>
  /** @type {boolean} */
  export let open = false;
  /** @type {string} */
  export let deckTitle = '';
  /** @type {{ rating: number, comment: string | null, display_name: string | null, email: string | null }[]} */
  export let reviews = [];
  /** @type {boolean} */
  export let loading = false;
  export let onClose = () => {};

  function handleBackdropClick(e) {
    if (e.target === e.currentTarget) onClose();
  }

  function handleKeydown(e) {
    if (e.key === 'Escape') onClose();
  }

  function reviewerName(r) {
    return (r.display_name && r.display_name.trim()) || r.email || 'Anonymous';
  }
</script>

{#if open}
  <!-- svelte-ignore a11y_no_noninteractive_element_interactions -->
  <div
    class="modal-backdrop"
    role="dialog"
    aria-modal="true"
    aria-labelledby="reviews-modal-title"
    onclick={handleBackdropClick}
    onkeydown={handleKeydown}
  >
    <div class="modal" onclick={(e) => e.stopPropagation()}>
      <h2 id="reviews-modal-title" class="modal-title">Reviews: {deckTitle}</h2>
      {#if loading}
        <p class="modal-message">Loading…</p>
      {:else if reviews.length === 0}
        <p class="modal-message">No reviews yet.</p>
      {:else}
        <ul class="reviews-list">
          {#each reviews as review (review.user_id)}
            <li class="review-item">
              <div class="review-header">
                <span class="review-stars" aria-label="{review.rating} out of 5 stars">
                  {['★', '★', '★', '★', '★'].map((_, i) => (i < review.rating ? '★' : '☆')).join('')}
                </span>
                <span class="reviewer-name">{reviewerName(review)}</span>
              </div>
              {#if review.comment}
                <p class="review-comment">{review.comment}</p>
              {/if}
            </li>
          {/each}
        </ul>
      {/if}
      <div class="modal-actions">
        <button type="button" class="btn btn-primary" onclick={onClose}>Close</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: 440px;
    width: 100%;
    max-height: 85vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
  }

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

  .modal-message {
    margin: 0 0 1.25rem 0;
    font-size: 0.95rem;
    color: var(--text-muted, #a0a0a0);
    line-height: 1.5;
  }

  .reviews-list {
    list-style: none;
    margin: 0 0 1.25rem 0;
    padding: 0;
    overflow-y: auto;
    flex: 1;
    min-height: 0;
  }

  .review-item {
    padding: 0.75rem 0;
    border-bottom: 1px solid var(--border, #333);
  }

  .review-item:last-child {
    border-bottom: none;
  }

  .review-header {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-wrap: wrap;
  }

  .review-stars {
    color: #eab308;
    letter-spacing: 0.05em;
    font-size: 1rem;
  }

  .reviewer-name {
    font-size: 0.9rem;
    font-weight: 500;
    color: var(--text-primary, #f0f0f0);
  }

  .review-comment {
    margin: 0.35rem 0 0 0;
    font-size: 0.9rem;
    color: var(--text-muted, #a0a0a0);
    line-height: 1.45;
  }

  .modal-actions {
    flex-shrink: 0;
  }

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

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

  .btn-primary:hover {
    background: #2563eb;
    border-color: #2563eb;
  }
</style>
