<script lang="ts">
  interface Props {
    visible?: boolean;
    percent?: number;
    stage?: string;
    message?: string;
  }

  let { visible = false, percent = 0, stage = '', message = '' }: Props = $props();

  // Map internal stage names to user-friendly labels
  const stageLabels: Record<string, string> = {
    'pipeline': 'Pipeline',
    'loading_model': 'Loading Model',
    'transcribing': 'Transcribing',
    'loading_diarization': 'Loading Diarization',
    'diarizing': 'Speaker Detection',
    'done': 'Complete',
  };

  // Pipeline steps for the task list
  const pipelineSteps = [
    { key: 'loading_model', label: 'Load transcription model' },
    { key: 'transcribing', label: 'Transcribe audio' },
    { key: 'loading_diarization', label: 'Load speaker detection model' },
    { key: 'diarizing', label: 'Identify speakers' },
    { key: 'merging', label: 'Merge results' },
  ];

  function getStepStatus(stepKey: string, currentStage: string): 'pending' | 'active' | 'done' {
    const stepOrder = pipelineSteps.map(s => s.key);
    const currentIdx = stepOrder.indexOf(currentStage);
    const stepIdx = stepOrder.indexOf(stepKey);

    if (currentStage === 'done') return 'done';
    if (stepIdx < currentIdx) return 'done';
    if (stepIdx === currentIdx) return 'active';
    return 'pending';
  }

  let displayStage = $derived(stageLabels[stage] || stage || 'Processing...');
</script>

{#if visible}
  <div class="overlay">
    <div class="progress-card">
      <div class="spinner-row">
        <div class="spinner"></div>
        <h3>{displayStage}</h3>
      </div>

      <div class="steps">
        {#each pipelineSteps as step}
          {@const status = getStepStatus(step.key, stage)}
          <div class="step" class:step-done={status === 'done'} class:step-active={status === 'active'}>
            <span class="step-icon">
              {#if status === 'done'}
                ✓
              {:else if status === 'active'}
                ⟳
              {:else}
                ·
              {/if}
            </span>
            <span class="step-label">{step.label}</span>
          </div>
        {/each}
      </div>

      <p class="status-text">{message || 'Please wait...'}</p>
      <p class="hint-text">This may take several minutes for large files</p>
    </div>
  </div>
{/if}

<style>
  .overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
  }
  .progress-card {
    background: #16213e;
    padding: 2rem 2.5rem;
    border-radius: 12px;
    min-width: 380px;
    max-width: 440px;
    color: #e0e0e0;
    border: 1px solid #2a3a5e;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
  }
  .spinner-row {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 1.25rem;
  }
  .spinner {
    width: 20px;
    height: 20px;
    border: 3px solid #2a3a5e;
    border-top-color: #e94560;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    flex-shrink: 0;
  }
  @keyframes spin {
    to { transform: rotate(360deg); }
  }
  h3 {
    margin: 0;
    font-size: 1.1rem;
  }
  .steps {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    margin-bottom: 1rem;
  }
  .step {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.85rem;
    color: #555;
  }
  .step-done {
    color: #4ecdc4;
  }
  .step-active {
    color: #e0e0e0;
    font-weight: 500;
  }
  .step-icon {
    width: 1.2rem;
    text-align: center;
    flex-shrink: 0;
  }
  .step-active .step-icon {
    animation: spin 1.5s linear infinite;
    display: inline-block;
  }
  .status-text {
    margin: 0.75rem 0 0;
    font-size: 0.85rem;
    color: #b0b0b0;
  }
  .hint-text {
    margin: 0.5rem 0 0;
    font-size: 0.75rem;
    color: #555;
  }
</style>
