<template>
  <div class="stats">
    <h2 class="page-title">Statistiken</h2>

    <div v-if="loading" class="loading">
      <p>Laden...</p>
    </div>

    <div v-else>
      <!-- Gesamt-Statistiken -->
      <div class="stats-grid grid grid-2">
        <div class="stat-card card">
          <div class="stat-icon">📊</div>
          <div class="stat-value">{{ stats.totalEntries || 0 }}</div>
          <div class="stat-label">Gesamt Einträge</div>
        </div>

        <div class="stat-card card">
          <div class="stat-icon">✅</div>
          <div class="stat-value">{{ stats.completedEntries || 0 }}</div>
          <div class="stat-label">Abgeschlossene Einträge</div>
        </div>

        <div class="stat-card card">
          <div class="stat-icon">🔴</div>
          <div class="stat-value">{{ stats.runningEntries || 0 }}</div>
          <div class="stat-label">Laufende Einträge</div>
        </div>

        <div class="stat-card card">
          <div class="stat-icon">⏱️</div>
          <div class="stat-value">{{ stats.totalHours || '0.00' }} h</div>
          <div class="stat-label">Gesamtstunden</div>
        </div>
      </div>

      <!-- Projekt-Statistiken -->
      <div class="project-stats">
        <h3>Statistiken nach Projekt</h3>
        <div v-if="Object.keys(stats.projectStats || {}).length === 0" class="empty-state card">
          <p>Noch keine Projekt-Statistiken verfügbar</p>
        </div>
        <div v-else class="project-list">
          <div 
            v-for="(data, project) in stats.projectStats" 
            :key="project"
            class="project-card card"
          >
            <div class="project-header">
              <h4>{{ project }}</h4>
              <span class="project-count">{{ data.count }} Einträge</span>
            </div>
            <div class="project-duration">
              <span class="duration-label">Gesamtdauer:</span>
              <span class="duration-value">{{ formatDuration(data.duration) }}</span>
            </div>
            <div class="project-hours">
              {{ (data.duration / 3600).toFixed(2) }} Stunden
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useTimeStore } from '../stores/timeStore'

const timeStore = useTimeStore()
const stats = ref({})
const loading = ref(true)

const formatDuration = (seconds) => {
  if (!seconds) return '00:00:00'
  const hours = Math.floor(seconds / 3600)
  const minutes = Math.floor((seconds % 3600) / 60)
  const secs = seconds % 60
  return `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(secs).padStart(2, '0')}`
}

onMounted(async () => {
  loading.value = true
  stats.value = await timeStore.fetchStats()
  loading.value = false
})
</script>

<style scoped>
.page-title {
  font-size: 1.8rem;
  margin-bottom: 1.5rem;
  color: #333;
  font-weight: normal;
}

.loading {
  text-align: center;
  padding: 2rem;
  color: #666;
}

.stats-grid {
  margin-bottom: 2rem;
}

.stat-card {
  text-align: center;
  padding: 1.5rem;
  border: 1px solid #d4d4d4;
}

.stat-icon {
  font-size: 2.5rem;
  margin-bottom: 0.5rem;
}

.stat-value {
  font-size: 2rem;
  font-weight: bold;
  color: #000;
  margin-bottom: 0.5rem;
}

.stat-label {
  color: #666;
  font-weight: normal;
  font-size: 1rem;
}

.project-stats h3 {
  font-size: 1.3rem;
  margin-bottom: 1rem;
  color: #333;
  font-weight: normal;
}

.empty-state {
  text-align: center;
  padding: 2rem;
  color: #666;
}

.project-list {
  display: grid;
  gap: 1rem;
}

.project-card {
  padding: 1.5rem;
}

.project-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
  padding-bottom: 0.75rem;
  border-bottom: 1px solid #d4d4d4;
}

.project-header h4 {
  margin: 0;
  color: #000;
  font-size: 1.1rem;
  font-weight: bold;
}

.project-count {
  background-color: #f0f0f0;
  padding: 0.2rem 0.6rem;
  border-radius: 3px;
  font-size: 0.85rem;
  color: #666;
  font-weight: normal;
  border: 1px solid #d4d4d4;
}

.project-duration {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.5rem;
}

.duration-label {
  font-weight: normal;
  color: #333;
}

.duration-value {
  font-family: 'Courier New', monospace;
  font-weight: bold;
  color: #5cb85c;
  font-size: 1.1rem;
}

.project-hours {
  color: #666;
  font-size: 0.9rem;
}
</style>

