<template>
  <div class="parallel-messages">
    <!-- Execution mode indicator -->
    <div class="mode-indicator" :class="mode">
      <span v-if="mode === 'parallel'">⚡ Parallel Execution</span>
      <span v-else>📋 Sequential Execution</span>
    </div>
    
    <!-- Round info -->
    <div v-if="roundInfo.current" class="round-info">
      Round {{ roundInfo.current }} / {{ roundInfo.max || '?' }}
    </div>
    
    <!-- Parallel message grid -->
    <div v-if="mode === 'parallel'" class="parallel-grid">
      <div 
        v-for="(agent, agentId) in agents" 
        :key="agentId"
        class="parallel-card"
        :class="agent.status"
      >
        <!-- Agent header -->
        <div class="card-header">
          <span class="agent-avatar" :style="{ background: agent.message?.sender_color || '#2563eb' }">
            {{ agent.name?.charAt(0) || '?' }}
          </span>
          <span class="agent-name">{{ agent.name }}</span>
          <span class="status-badge" :class="agent.status">
            {{ statusLabels[agent.status] || agent.status }}
          </span>
        </div>
        
        <!-- Progress bar -->
        <div v-if="agent.status === 'streaming'" class="progress-bar">
          <div class="progress-fill" :style="{ width: agent.progress + '%' }"></div>
        </div>
        
        <!-- Message content -->
        <div class="card-body">
          <div v-if="agent.status === 'pending'" class="pending-state">
            <div class="spinner-small"></div>
            <span>Pending...</span>
          </div>
          
          <div v-else-if="agent.status === 'streaming'" class="streaming-content">
            <div v-if="agent.message?.process_steps?.length" class="process-steps">
              <div v-for="(step, idx) in agent.message.process_steps" :key="idx" class="step">
                {{ step.content }}
              </div>
            </div>
            <div v-else-if="agent.message?.content" class="streaming-text">
              {{ agent.message.content }}<span class="cursor">▌</span>
            </div>
            <div v-else class="typing-indicator">
              <span></span><span></span><span></span>
            </div>
          </div>
          
          <div v-else-if="agent.status === 'completed'" class="completed-content">
            <MessageBubble v-if="agent.message" :message="agent.message" />
          </div>
          
          <div v-else-if="agent.status === 'error'" class="error-state">
            ⚠️ {{ agent.error || 'An error occurred' }}
          </div>
        </div>
      </div>
    </div>
    
    <!-- Sequential message list (compatibility mode) -->
    <div v-else class="sequential-list">
      <MessageBubble 
        v-for="msg in messages" 
        :key="msg.id"
        :message="msg"
      />
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useParallelStreamStore } from '../utils/parallelStreamStore.js'
import MessageBubble from './MessageBubble.vue'

const props = defineProps({
  roomId: { type: String, required: true },
  mode: { type: String, default: 'sequential' },
  messages: { type: Array, default: () => [] }
})

const store = useParallelStreamStore()

const agents = computed(() => store.rooms[props.roomId]?.agents || {})
const roundInfo = computed(() => store.rooms[props.roomId]?.roundInfo || { current: 0, max: 0 })

const statusLabels = {
  pending: 'Pending',
  streaming: 'Generating',
  completed: 'Done',
  error: 'Error'
}
</script>

<style scoped>
.parallel-messages {
  padding: 16px;
}

.mode-indicator {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 16px;
}

.mode-indicator.parallel {
  background: linear-gradient(135deg, rgba(59, 130, 246, 0.1), rgba(139, 92, 246, 0.1));
  color: var(--mode-parallel-color, #3b82f6);
  border: 1px solid rgba(59, 130, 246, 0.2);
}

.mode-indicator.sequential {
  background: rgba(107, 114, 128, 0.1);
  color: var(--mode-sequential-color, #6b7280);
  border: 1px solid rgba(107, 114, 128, 0.2);
}

.round-info {
  font-size: 12px;
  color: var(--text-secondary);
  margin-bottom: 12px;
}

/* Parallel grid layout */
.parallel-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 16px;
  padding: 16px 0;
}

@media (max-width: 768px) {
  .parallel-grid {
    grid-template-columns: 1fr;
  }
}

/* Parallel card */
.parallel-card {
  background: var(--bg-primary, #ffffff);
  border: 1px solid var(--border-color, #e5e7eb);
  border-radius: var(--parallel-card-radius, 12px);
  overflow: hidden;
  transition: all var(--transition-normal, 300ms ease);
}

.parallel-card:hover {
  box-shadow: var(--parallel-card-shadow-active, 0 0 0 2px rgba(59, 130, 246, 0.3));
}

/* Status variants */
.parallel-card.streaming {
  border-color: var(--status-streaming-border, #3b82f6);
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
  animation: pulse-border 2s infinite;
}

.parallel-card.completed {
  border-color: var(--status-completed-border, #10b981);
}

.parallel-card.error {
  border-color: var(--status-error-border, #ef4444);
}

@keyframes pulse-border {
  0%, 100% { box-shadow: 0 0 0 0 rgba(59, 130, 246, 0.4); }
  50% { box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.1); }
}

/* Card header */
.card-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: var(--bg-secondary, #f9fafb);
  border-bottom: 1px solid var(--border-color, #e5e7eb);
}

.agent-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: 600;
  font-size: 14px;
  flex-shrink: 0;
}

.agent-name {
  font-weight: 500;
  color: var(--text-primary, #111827);
  flex: 1;
}

/* Status badge */
.status-badge {
  padding: 4px 10px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
}

.status-badge.pending {
  background: var(--status-pending-bg, #f3f4f6);
  color: var(--status-pending-text, #6b7280);
}

.status-badge.streaming {
  background: var(--status-streaming-bg, #eff6ff);
  color: var(--status-streaming-text, #3b82f6);
  animation: badge-pulse 1.5s infinite;
}

.status-badge.completed {
  background: var(--status-completed-bg, #ecfdf5);
  color: var(--status-completed-text, #10b981);
}

.status-badge.error {
  background: var(--status-error-bg, #fef2f2);
  color: var(--status-error-text, #ef4444);
}

@keyframes badge-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.7; }
}

/* Progress bar */
.progress-bar {
  height: var(--progress-height, 4px);
  background: var(--progress-bg, #e5e7eb);
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: var(--progress-fill, linear-gradient(90deg, #3b82f6, #8b5cf6));
  transition: width var(--transition-normal, 300ms ease);
  border-radius: 0 2px 2px 0;
}

/* Card body */
.card-body {
  padding: 16px;
  min-height: 120px;
  max-height: 400px;
  overflow-y: auto;
}

/* Pending state */
.pending-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  height: 100%;
  min-height: 100px;
  color: var(--text-secondary, #6b7280);
}

.spinner-small {
  width: 24px;
  height: 24px;
  border: 2px solid var(--border-color, #e5e7eb);
  border-top-color: var(--primary-color, #3b82f6);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Streaming content */
.streaming-content {
  position: relative;
}

.streaming-text {
  word-wrap: break-word;
  white-space: pre-wrap;
}

.cursor {
  animation: blink 1s infinite;
  color: var(--primary-color, #3b82f6);
}

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

/* Typing indicator */
.typing-indicator {
  display: flex;
  gap: 4px;
  padding: 8px 0;
}

.typing-indicator span {
  width: var(--typing-dot-size, 6px);
  height: var(--typing-dot-size, 6px);
  border-radius: 50%;
  background: var(--primary-color, #3b82f6);
  animation: typing-bounce var(--typing-animation-duration, 1s) infinite;
}

.typing-indicator span:nth-child(2) {
  animation-delay: 0.2s;
}

.typing-indicator span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes typing-bounce {
  0%, 100% {
    opacity: 0.3;
    transform: scale(0.8) translateY(0);
  }
  50% {
    opacity: 1;
    transform: scale(1) translateY(-4px);
  }
}

/* Error state */
.error-state {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px;
  background: var(--status-error-bg, #fef2f2);
  border-radius: 8px;
  color: var(--status-error-text, #ef4444);
  font-size: 14px;
}

/* Completed content */
.completed-content {
  animation: fade-in 0.3s ease;
}

@keyframes fade-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Sequential list */
.sequential-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* Process steps */
.process-steps {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.step {
  padding: 8px;
  background: var(--bg-secondary, #f9fafb);
  border-radius: 4px;
  font-size: 14px;
}
</style>