<script lang="ts">
  import { onMount } from 'svelte';
  import { getOrCreateGameState } from '$lib/db/gameState';
  import type { GameState } from '$lib/db/schema';
  
  let gameState: GameState | null = $state(null);

  // Progress bar color based on rarity score
  const progressColor = $derived.by(() => {
    const score = gameState?.rarityScore ?? 0;
    return getProgressColor(score);
  });

  function getProgressColor(score: number): string {
    if (score < 100) return '#4caf50'; // green - beginner
    if (score < 500) return '#ffc107'; // yellow - intermediate
    if (score < 1000) return '#ff9800'; // orange - advanced
    if (score < 2000) return '#f44336'; // red - expert
    return '#9c27b0'; // purple - master
  }
  
  onMount(() => {
    // Load game state
    getOrCreateGameState().then(state => {
      gameState = state;
    });

    // Refresh game state periodically
    const interval = setInterval(() => {
      getOrCreateGameState().then(state => {
        gameState = state;
      });
    }, 5000); // Update every 5 seconds

    return () => clearInterval(interval);
  });
</script>

{#if gameState}
  <div class="progress-container">
    <div class="progress-bar">
      <div class="progress-fill" style="width: {Math.min((gameState.rarityScore / 2000) * 100, 100)}%; background-color: {progressColor}"></div>
    </div>
    <span class="progress-stats">📷 {gameState.totalDiscoveries} 🔍 {gameState.uniqueDiscoveries}</span>
  </div>
{/if}

<style>
  .progress-container {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.4rem 1rem;
    padding-top: env(safe-area-inset-top);
    background: white;
    border-bottom: 1px solid #f0f0f0;
  }
  
  @media (max-width: 768px) {
    .progress-container {
      padding: 0.3rem 1rem;
      padding-top: env(safe-area-inset-top);
    }
  }
  
  .progress-bar {
    margin-top: 5px;
    flex: 1;
    background: #f0f0f0;
    height: 6px;
    border-radius: 3px;
    overflow: hidden;
  }
  
  .progress-fill {
    height: 100%;
    transition: width 0.8s ease;
  }
  
  .progress-stats {
    font-size: 0.875rem;
    color: #666;
    font-weight: 500;
    white-space: nowrap;
  }
</style>