<template>
  <!-- Loading state as a single card -->
  <div v-if="loading" class="card glass">
    <div class="card-header">
      <h2 class="card-title">Eventos y comparación</h2>
    </div>
    <div class="placeholder">Cargando datos…</div>
  </div>

  <!-- Count/percent view in a single card -->
  <div v-else-if="viewMode !== 'ratio'" class="card glass">
    <div class="card-header">
      <h2 class="card-title">Eventos y comparación</h2>
      <div v-if="filtersCollapsed && (activeFilters?.hasFilters || selectedPlayerUuid)" class="active-filters-summary">
        <span class="filter-tag" v-if="activeFilters?.dataSource !== 'aggregated'">
          {{ activeFilters?.dataSource === 'active-rooms' ? '🔴 Tiempo Real' : '📁 Agregado' }}
        </span>
        <span class="filter-tag" v-if="activeFilters?.round !== 'all'">
          Ronda {{ activeFilters?.round }}
        </span>
        <span class="filter-tag" v-if="activeFilters?.game !== 'all'">
          {{ activeFilters?.game }}
        </span>
        <span class="filter-tag player-tag" v-if="selectedPlayerUuid && activeFilters?.selectedPlayer">
          👤 {{ activeFilters.selectedPlayer }}
        </span>
        <span class="filter-tag room-tag" v-if="activeFilters?.selectedRoom">
          🏠 {{ activeFilters.selectedRoom }}
        </span>
      </div>
    </div>
    <div class="bars big">
      <div 
        v-for="eventType in eventTypes" 
        :key="eventType" 
        class="bar-row" 
        :class="{ highlight: highlighted === eventType }" 
        @mouseenter="highlighted = eventType" 
        @mouseleave="highlighted = ''"
      >
        <div class="bar">
          <div 
            class="bar-fill global shimmer" 
            :style="{ 
              width: globalBarWidth(eventType) + '%', 
              background: eventStyles[eventType]?.gradient || 'linear-gradient(90deg, #94a3b8, #64748b)' 
            }"
          ></div>
          <div 
            v-if="selectedPlayerUuid" 
            class="bar-fill player" 
            :style="{ 
              width: playerBarWidth(eventType) + '%', 
              background: playerBarGradient 
            }"
          ></div>
          <div 
            class="bar-chip" 
            :style="{ 
              background: getEventChipBg(eventType), 
              borderColor: getEventBorderColor(eventType) 
            }"
          >
            <span class="event-icon">{{ eventStyles[eventType]?.icon || '📊' }}</span>
            <span class="chip-label">{{ friendlyEventName(eventType) }}</span>
            <span class="chip-count global">{{ globalValueLabel(eventType) }}</span>
            <span v-if="selectedPlayerUuid" class="chip-count player">{{ playerValueLabel(eventType) }}</span>
          </div>
        </div>
      </div>
      <div class="hint small">Basado en mensajes disponibles por sala. Haz clic en un jugador para comparar.</div>
    </div>
  </div>

  <!-- Ratio view: one card per group -->
  <div v-else class="ratio-cards">
    <div 
      v-for="group in ratioData" 
      :key="group.name"
      v-show="group.total > 0"
      class="card glass ratio-card"
      :class="{ highlight: highlighted === group.name }"
      @mouseenter="highlighted = group.name" 
      @mouseleave="highlighted = ''"
    >
      <div class="card-header">
        <h3 class="card-title">{{ group.name }}</h3>
        <span class="group-total">{{ group.total }}</span>
      </div>
      <div class="ratio-bar">
        <div 
          v-for="(action, actionIndex) in group.actions" 
          :key="action"
          class="ratio-segment"
          :style="{
            width: group.percentages[actionIndex] + '%',
            background: eventStyles[action]?.gradient || 'linear-gradient(90deg, #94a3b8, #64748b)'
          }"
        >
          <div 
            class="ratio-event-chip"
            v-if="group.percentages[actionIndex] > 5"
            :style="{
              background: getEventChipBg(action),
              borderColor: getEventBorderColor(action)
            }"
          >
            <span class="ratio-icon">{{ eventStyles[action]?.icon || '📊' }}</span>
            <span class="ratio-label">{{ group.labels[actionIndex] }}</span>
            <span class="ratio-count">{{ group.values[actionIndex] }} ({{ Math.round(group.percentages[actionIndex]) }}%)</span>
          </div>
        </div>
      </div>
    </div>
    <div class="hint small">
      {{ selectedPlayerUuid ? 'Proporciones del jugador seleccionado' : 'Proporciones globales' }}. 
      Los segmentos muestran la proporción relativa dentro de cada categoría.
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';

interface Props {
  eventTypes: string[];
  eventStyles: Record<string, { icon: string; color: string; gradient: string }>;
  globalEventCounts: Record<string, number>;
  playerEventCounts: Record<string, number>;
  selectedPlayerUuid?: string;
  playerBarGradient: string;
  viewMode: 'count' | 'percent' | 'ratio';
  loading?: boolean;
  filtersCollapsed?: boolean;
  activeFilters?: {
    dataSource: string;
    round: string;
    game: string;
    hasFilters: boolean;
    selectedPlayer?: string;
    selectedRoom?: string;
  };
  groupTotals?: {
    offers: number;
    responses: number;
    force: number;
    shame: number;
    report: number;
    averageScore: number;
    totalPlayers: number;
  };
}

const props = withDefaults(defineProps<Props>(), {
  selectedPlayerUuid: '',
  loading: false
});

const highlighted = ref('');

// Define ratio groups for superposed view
const ratioGroups = computed(() => [
  {
    name: 'Ofertas',
    actions: ['p1_propose', 'p1_no_offer'],
    labels: ['Ofrecer', 'No Ofrecer'],
    total: props.groupTotals?.offers || 0
  },
  {
    name: 'Respuestas',
    actions: ['p2_accept', 'p2_reject', 'p2_snatch'],
    labels: ['Aceptar', 'Rechazar', 'Robar'],
    total: props.groupTotals?.responses || 0
  },
  {
    name: 'Forzar',
    actions: ['p2_force', 'p2_no_force'],
    labels: ['Forzar', 'No Forzar'],
    total: props.groupTotals?.force || 0
  },
  {
    name: 'Avergonzar',
    actions: ['p1_shame', 'p1_no_shame'],
    labels: ['Asignar', 'No Asignar'],
    total: props.groupTotals?.shame || 0
  },
  {
    name: 'Denunciar',
    actions: ['p1_report', 'p1_no_report'],
    labels: ['Denunciar', 'No Denunciar'],
    total: props.groupTotals?.report || 0
  },
  {
    name: 'Puntaje Promedio',
    actions: ['score_p1', 'score_p2'],
    labels: ['P1', 'P2'],
    total: props.groupTotals?.averageScore ? props.groupTotals.averageScore.toFixed(1) : '0.0'
  },
  {
    name: 'Total Jugadores',
    actions: ['players_with_shame', 'players_without_shame'],
    labels: ['Con vergüenza', 'Sin vergüenza'],
    total: props.groupTotals?.totalPlayers || 0,
    isCustomRatio: true // Special handling needed
  }
]);

// Compute ratio data for each group
const ratioData = computed(() => {
  return ratioGroups.value.map(group => {
    const counts = props.selectedPlayerUuid 
      ? props.playerEventCounts 
      : props.globalEventCounts;
    
    let values = group.actions.map(action => counts[action] || 0);
    
    // Special handling for players ratio (shame vs no shame)
    if (group.isCustomRatio && group.name === 'Total Jugadores') {
      const playersWithShame = counts['players_with_shame'] || 0;
      const playersWithoutShame = counts['players_without_shame'] || 0;
      values = [playersWithShame, playersWithoutShame];
    }
    
    const total = values.reduce((sum, val) => sum + val, 0);
    
    return {
      ...group,
      values,
      total,
      percentages: total > 0 ? values.map(val => (val / total) * 100) : values.map(() => 0)
    };
  });
});

// Global calculations
const globalMax = computed(() => {
  const vals = props.eventTypes.map(k => props.globalEventCounts[k] || 0);
  const m = Math.max(0, ...vals);
  return m || 1;
});

const globalTotal = computed(() => 
  props.eventTypes.reduce((acc, k) => acc + (props.globalEventCounts[k] || 0), 0) || 1
);

function globalBarWidth(eventType: string) {
  const v = props.globalEventCounts[eventType] || 0;
  return Math.round((v / (props.viewMode === 'percent' ? globalTotal.value : globalMax.value)) * 100);
}

function globalValueLabel(eventType: string) {
  const v = props.globalEventCounts[eventType] || 0;
  return props.viewMode === 'percent' ? `${Math.round((v / globalTotal.value) * 100)}%` : String(v);
}

// Player calculations
const playerMax = computed(() => {
  const vals = props.eventTypes.map(k => props.playerEventCounts[k] || 0);
  const m = Math.max(0, ...vals);
  return m || 1;
});

const playerTotal = computed(() => 
  props.eventTypes.reduce((acc, k) => acc + (props.playerEventCounts[k] || 0), 0) || 1
);

function playerBarWidth(eventType: string) {
  const v = props.playerEventCounts[eventType] || 0;
  return Math.round((v / (props.viewMode === 'percent' ? playerTotal.value : playerMax.value)) * 100);
}

function playerValueLabel(eventType: string) {
  const v = props.playerEventCounts[eventType] || 0;
  return props.viewMode === 'percent' ? `${Math.round((v / playerTotal.value) * 100)}%` : String(v);
}

// Styling helpers
function getEventChipBg(eventType: string): string {
  const style = props.eventStyles[eventType];
  if (!style) return 'rgba(255,255,255,0.82)';
  return `linear-gradient(135deg, ${style.color}15 0%, rgba(255,255,255,0.9) 100%)`;
}

function getEventBorderColor(eventType: string): string {
  const style = props.eventStyles[eventType];
  if (!style) return 'rgba(229,231,235,0.9)';
  return `${style.color}40`;
}

function friendlyEventName(eventType: string): string {
  const friendlyNames: Record<string, string> = {
    p1_propose: 'Ofrecer',
    p1_no_offer: 'No Ofrecer',
    p2_snatch: 'Robar',
    p2_accept: 'Aceptar Oferta',
    p2_force: 'Forzar Oferta',
    p2_no_force: 'No Forzar Oferta',
    p2_reject: 'Rechazar Oferta',
    p1_shame: 'Asignar Vergüenza',
    p1_no_shame: 'No Asignar Vergüenza',
    p1_report: 'Denunciar',
    p1_no_report: 'No Denunciar',
  };
  return friendlyNames[eventType] || eventType;
}
</script>

<style scoped>
.card { 
  padding: 14px 16px; 
  display: flex; 
  flex-direction: column; 
  flex: 1 1 auto; 
  min-height: 0; 
}

.glass { 
  background: rgba(255, 255, 255, 0.92); 
  border: 1px solid rgba(229, 231, 235, 0.95); 
  box-shadow: 0 18px 50px rgba(0,0,0,0.12), inset 0 1px 0 rgba(255,255,255,0.75); 
  backdrop-filter: blur(18px) saturate(120%); 
  -webkit-backdrop-filter: blur(18px) saturate(120%); 
  border-radius: 16px; 
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
  flex-wrap: wrap;
  gap: 8px;
}
.card-title { 
  margin: 0; 
  color: #334155; 
}

.ratio-cards {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.active-filters-summary {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.filter-tag {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 700;
  box-shadow: 0 2px 6px rgba(102,126,234,0.25);
  white-space: nowrap;
}

.filter-tag.player-tag {
  background: linear-gradient(135deg, #10b981 0%, #059669 100%);
  box-shadow: 0 2px 6px rgba(16,185,129,0.25);
}

.placeholder { 
  color: #64748b; 
  padding: 12px; 
  border: 1px dashed #e5e9f0; 
  border-radius: 10px; 
  background: #fff; 
}

.bars { 
  display: flex; 
  flex-direction: column; 
  gap: 8px; 
  flex: 1 1 auto; 
  min-height: 0; 
}

.bars.big { 
  height: 100%; 
}

.bars.big .bar-row { 
  flex: 1 1 0; 
  min-height: 36px; 
}

.bar-row { 
  display: flex; 
  align-items: stretch; 
  padding: 0; 
  background: transparent; 
  transition: transform .18s ease; 
}

.bar-row.highlight { 
  transform: translateX(4px); 
}

.bar { 
  position: relative; 
  height: 100%; 
  background: linear-gradient(135deg, rgba(238,242,255,0.4) 0%, rgba(199,210,254,0.2) 100%); 
  border-radius: 12px; 
  overflow: hidden; 
  border: 1px solid rgba(199,210,254,0.3); 
  width: 100%; 
}

.bar-fill { 
  position: absolute; 
  left: 0; 
  top: 0; 
  height: 100%; 
  transform-origin: left center; 
  transition: width .65s cubic-bezier(.2,.7,.1,1); 
  border-radius: 12px; 
}

.bar-fill.global { 
  backdrop-filter: blur(4px); 
  opacity: 0.75; 
}

.bar-fill.player { 
  mix-blend-mode: normal; 
  opacity: 0.85; 
  backdrop-filter: blur(4px); 
}

.bar-chip { 
  position: absolute; 
  left: 50%; 
  top: 50%; 
  transform: translate(-50%, -50%); 
  display: flex; 
  align-items: center; 
  gap: 6px; 
  padding: 5px 10px; 
  border-radius: 999px; 
  border: 1px solid; 
  box-shadow: 0 4px 12px rgba(0,0,0,0.08), inset 0 1px 0 rgba(255,255,255,0.4); 
  backdrop-filter: blur(8px) saturate(120%); 
  -webkit-backdrop-filter: blur(8px) saturate(120%); 
  transition: all 0.3s ease;
  min-width: 160px;
}

.bar-row:hover .bar-chip { 
  transform: translate(-50%, -50%) scale(1.05); 
  box-shadow: 0 6px 16px rgba(0,0,0,0.12), inset 0 1px 0 rgba(255,255,255,0.6); 
}

.event-icon { 
  font-size: 16px; 
}

.chip-label { 
  font-weight: 800; 
  color: #0f172a; 
  letter-spacing: .1px; 
  white-space: nowrap; 
  font-size: 14px; 
  flex: 1;
  text-align: center;
}

.chip-count { 
  padding: 3px 8px; 
  border-radius: 999px; 
  font-weight: 800; 
  font-size: 12px; 
  box-shadow: 0 1px 3px rgba(0,0,0,0.06); 
  margin-left: 4px; 
}

.chip-count.global { 
  background: rgba(255,255,255,0.7); 
  color: #1f2937; 
  border: 1px solid rgba(229,231,235,0.5); 
}

.chip-count.player { 
  background: rgba(99,102,241,0.15); 
  color: #312e81; 
  border: 1px solid rgba(99,102,241,0.3); 
}

/* Shimmer on global bars for subtle movement */
.shimmer::after { 
  content: ''; 
  position: absolute; 
  inset: 0; 
  background: linear-gradient(110deg, transparent 30%, rgba(255,255,255,0.25) 50%, transparent 70%); 
  transform: translateX(-100%); 
  animation: shimmer 3.2s ease-in-out infinite; 
}

@keyframes shimmer { 
  0% { transform: translateX(-100%); } 
  45% { transform: translateX(110%); } 
  100% { transform: translateX(110%); } 
}

.hint.small { 
  font-size: 12px; 
  color: #64748b; 
}

/* Ratio bars styles */
.ratio-bars {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 8px 0;
}

.ratio-group {
  flex: 0 0 auto;
  min-height: 120px;
  margin-bottom: 8px;
  transition: transform .18s ease;
}

.ratio-group.highlight {
  transform: translateX(4px);
}

/* Group header styles */
.ratio-group-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 12px;
  margin-bottom: 12px;
  padding: 8px 12px;
  background: rgba(255, 255, 255, 0.6);
  border: 1px solid rgba(229, 231, 235, 0.4);
  border-radius: 10px;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

.group-title {
  margin: 0;
  font-size: 18px;
  font-weight: 800;
  color: #1e293b;
  letter-spacing: -0.025em;
}

.group-total {
  font-size: 16px;
  font-weight: 900;
  color: #667eea;
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.15) 0%, rgba(102, 126, 234, 0.08) 100%);
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid rgba(102, 126, 234, 0.25);
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.15);
  min-width: 48px;
  text-align: center;
}

/* Place the group total just next to the title */
.ratio-card .card-header {
  justify-content: flex-start;
  gap: 8px;
}
.ratio-card .group-total {
  margin-left: 6px;
}

.ratio-bar {
  position: relative;
  height: 60px;
  background: linear-gradient(135deg, rgba(238,242,255,0.4) 0%, rgba(199,210,254,0.2) 100%);
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid rgba(199,210,254,0.3);
  width: 100%;
  display: flex;
}

.ratio-segment {
  height: 100%;
  transition: all 0.6s cubic-bezier(.2,.7,.1,1);
  backdrop-filter: blur(4px);
  opacity: 0.8;
}

.ratio-segment:first-child {
  border-top-left-radius: 12px;
  border-bottom-left-radius: 12px;
}

.ratio-segment:last-child {
  border-top-right-radius: 12px;
  border-bottom-right-radius: 12px;
}

.ratio-event-chip {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  border-radius: 999px;
  border: 1px solid;
  box-shadow: 0 4px 12px rgba(0,0,0,0.08), inset 0 1px 0 rgba(255,255,255,0.4);
  backdrop-filter: blur(8px) saturate(120%);
  -webkit-backdrop-filter: blur(8px) saturate(120%);
  transition: all 0.3s ease;
  white-space: nowrap;
  max-width: 95%;
}

.ratio-segment:hover .ratio-event-chip {
  transform: translate(-50%, -50%) scale(1.05);
  box-shadow: 0 6px 16px rgba(0,0,0,0.12), inset 0 1px 0 rgba(255,255,255,0.6);
}

.ratio-icon {
  font-size: 14px;
}

.ratio-label {
  color: #0f172a;
  font-weight: 800;
  font-size: 13px;
  letter-spacing: .1px;
}

.ratio-count {
  color: #1f2937;
  font-weight: 800;
  background: rgba(255,255,255,0.7);
  border: 1px solid rgba(229,231,235,0.5);
  border-radius: 999px;
  padding: 2px 6px;
  font-size: 11px;
  margin-left: 2px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}

/* Responsive chip sizing */
@media (min-width: 1200px) {
  .bar-chip {
    min-width: 200px;
    padding: 6px 12px;
    gap: 8px;
  }
  
  .event-icon {
    font-size: 16px;
  }
  
  .chip-label {
    font-size: 14px;
  }
  
  .chip-count {
    padding: 3px 8px;
    font-size: 12px;
  }
}

@media (min-width: 768px) and (max-width: 1199px) {
  .bar-chip {
    min-width: 140px;
    padding: 5px 10px;
    gap: 6px;
  }
  
  .event-icon {
    font-size: 14px;
  }
  
  .chip-label {
    font-size: 12px;
  }
  
  .chip-count {
    padding: 2px 6px;
    font-size: 10px;
  }
}

@media (max-width: 767px) {
  .card { padding: 10px 12px; }
  .card-header { margin-bottom: 8px; }
  .card-title { font-size: 16px; }
  .bars.big .bar-row { min-height: 28px; }
  .ratio-group { min-height: 90px; }
  .ratio-bar { height: 42px; }
  .bar-chip {
    min-width: 120px;
    padding: 4px 8px;
    gap: 4px;
  }
  
  .event-icon {
    font-size: 12px;
  }
  
  .chip-label {
    font-size: 11px;
  }
  
  .chip-count {
    padding: 2px 4px;
    font-size: 9px;
    margin-left: 2px;
  }
}

@media (max-width: 480px) {
  .ratio-cards {
    gap: 8px;
  }
  .card {
    padding: 8px 10px;
  }

  .card-header {
    margin-bottom: 6px;
  }
  
  .card-title {
    font-size: 14px;
  }
  .bars.big .bar-row { min-height: 24px; }

  .group-total {
    font-size: 12px;
    padding: 3px 8px;
    min-width: 40px;
  }

  .ratio-bar {
    height: 34px;
  }

  .ratio-group {
    min-height: 74px;
  }

  .ratio-group-header {
    margin-top: 6px;
    margin-bottom: 6px;
    padding: 6px 8px;
  }

  .bar-chip {
    min-width: 100px;
    padding: 3px 6px;
    gap: 3px;
  }
  
  .event-icon {
    font-size: 11px;
  }
  
  .chip-label {
    font-size: 10px;
  }
  
  .chip-count {
    padding: 1px 3px;
    font-size: 8px;
    margin-left: 1px;
  }

  /* Ratio responsive styles */
  .ratio-event-chip {
    padding: 3px 6px;
    gap: 3px;
  }
  
  .ratio-icon {
    font-size: 11px;
  }
  
  .ratio-label {
    font-size: 10px;
  }
  
  .ratio-count {
    font-size: 8px;
    padding: 1px 3px;
  }

  .filter-tag {
    font-size: 10px;
    padding: 3px 6px;
  }

  .hint.small {
    display: none;
  }
}

@media (max-width: 767px) {
  .ratio-event-chip {
    padding: 3px 7px;
    gap: 4px;
  }
  
  .ratio-icon {
    font-size: 12px;
  }
  
  .ratio-label {
    font-size: 11px;
  }
  
  .ratio-count {
    font-size: 9px;
    padding: 1px 4px;
  }
}

@media (min-width: 768px) and (max-width: 1199px) {
  .ratio-event-chip {
    padding: 4px 8px;
    gap: 5px;
  }
  
  .ratio-icon {
    font-size: 13px;
  }
  
  .ratio-label {
    font-size: 12px;
  }
  
  .ratio-count {
    font-size: 10px;
  }
}

@media (min-width: 1200px) {
  .ratio-event-chip {
    padding: 6px 12px;
    gap: 8px;
  }
  
  .ratio-icon {
    font-size: 16px;
  }
  
  .ratio-label {
    font-size: 14px;
  }
  
  .ratio-count {
    font-size: 12px;
    padding: 3px 8px;
  }
}
</style>
