<script lang="ts">
  import { speakers } from '$lib/stores/transcript';
  import type { Speaker } from '$lib/types/transcript';

  let editingSpeakerId = $state<string | null>(null);
  let editName = $state('');

  function startRename(speaker: Speaker) {
    editingSpeakerId = speaker.id;
    editName = speaker.display_name || speaker.label;
  }

  function finishRename(speakerId: string) {
    const trimmed = editName.trim();
    if (trimmed) {
      speakers.update(list => list.map(s => {
        if (s.id !== speakerId) return s;
        return { ...s, display_name: trimmed };
      }));
    }
    editingSpeakerId = null;
  }

  function handleKeydown(e: KeyboardEvent, speakerId: string) {
    if (e.key === 'Enter') {
      e.preventDefault();
      finishRename(speakerId);
    } else if (e.key === 'Escape') {
      editingSpeakerId = null;
    }
  }
</script>

<div class="speaker-manager">
  <h3>Speakers</h3>
  {#if $speakers.length === 0}
    <p class="empty-hint">No speakers detected</p>
    <p class="setup-hint">
      Speaker detection requires a HuggingFace token.
      Set the <code>HF_TOKEN</code> environment variable and restart.
    </p>
  {:else}
    <ul class="speaker-list">
      {#each $speakers as speaker (speaker.id)}
        <li class="speaker-item">
          <span class="speaker-color" style="background: {speaker.color}"></span>
          {#if editingSpeakerId === speaker.id}
            <input
              class="rename-input"
              type="text"
              bind:value={editName}
              onblur={() => finishRename(speaker.id)}
              onkeydown={(e) => handleKeydown(e, speaker.id)}
            />
          {:else}
            <!-- svelte-ignore a11y_no_static_element_interactions -->
            <span class="speaker-name" ondblclick={() => startRename(speaker)}>
              {speaker.display_name || speaker.label}
            </span>
            <button class="rename-btn" onclick={() => startRename(speaker)} title="Rename speaker">
              ✏
            </button>
          {/if}
        </li>
      {/each}
    </ul>
    <p class="speaker-hint">Double-click a name to rename</p>
  {/if}
</div>

<style>
  .speaker-manager {
    padding: 1rem;
    background: #16213e;
    border-radius: 8px;
    color: #e0e0e0;
  }
  h3 {
    margin: 0 0 0.5rem;
    font-size: 0.95rem;
  }
  .empty-hint {
    color: #666;
    font-size: 0.875rem;
    margin-bottom: 0.25rem;
  }
  .setup-hint {
    color: #555;
    font-size: 0.75rem;
    line-height: 1.4;
  }
  .setup-hint code {
    background: rgba(233, 69, 96, 0.15);
    color: #e94560;
    padding: 0.1rem 0.3rem;
    border-radius: 3px;
    font-size: 0.7rem;
  }
  .speaker-list {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
  }
  .speaker-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.35rem 0.5rem;
    background: rgba(255,255,255,0.03);
    border-radius: 4px;
  }
  .speaker-color {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    flex-shrink: 0;
  }
  .speaker-name {
    flex: 1;
    cursor: pointer;
    font-size: 0.875rem;
  }
  .rename-btn {
    background: none;
    border: none;
    color: #666;
    cursor: pointer;
    font-size: 0.75rem;
    padding: 0.15rem 0.3rem;
    border-radius: 3px;
  }
  .rename-btn:hover {
    background: rgba(255,255,255,0.1);
    color: #e0e0e0;
  }
  .rename-input {
    flex: 1;
    background: #1a1a2e;
    color: #e0e0e0;
    border: 1px solid #e94560;
    border-radius: 3px;
    padding: 0.2rem 0.4rem;
    font-size: 0.875rem;
    font-family: inherit;
  }
  .rename-input:focus {
    outline: none;
    border-color: #ff6b81;
  }
  .speaker-hint {
    color: #555;
    font-size: 0.7rem;
    margin-top: 0.5rem;
    margin-bottom: 0;
  }
</style>
