---
export interface Props {
  averageRating: number;
  totalVotes: number;
  totalReviews: number;
  ratingDistribution: {
    5: number;
    4: number;
    3: number;
    2: number;
    1: number;
  };
}

const { averageRating, totalVotes, totalReviews, ratingDistribution } = Astro.props;

// Расчёт процентов для каждого рейтинга
const getPercentage = (count: number) => {
  if (totalReviews === 0) return 0;
  return Math.round((count / totalReviews) * 100);
};
---

<section class="voting-summary animate-on-scroll" data-animation="fade-up">
  <div class="summary-container">
    <h2 class="summary-title">Общая статистика отзывов</h2>
    
    <div class="summary-grid">
      <!-- Левая колонка - Общий рейтинг -->
      <div class="overall-rating">
        <div class="rating-big">
          <span class="rating-number">{averageRating.toFixed(1)}</span>
          <div class="rating-stars-big">
            {[1, 2, 3, 4, 5].map((star) => (
              <svg 
                xmlns="http://www.w3.org/2000/svg" 
                class={`star-icon ${star <= Math.round(averageRating) ? 'filled' : ''}`}
                viewBox="0 0 24 24" 
                fill={star <= Math.round(averageRating) ? 'currentColor' : 'none'} 
                stroke="currentColor"
              >
                <path d="M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z" />
              </svg>
            ))}
          </div>
        </div>
        <p class="total-votes-text">
          На основе <strong>{totalReviews}</strong> отзывов
        </p>
        <p class="total-reviews-text">
          <strong>{totalReviews}</strong> отзывов оставлено
        </p>
      </div>

      <!-- Правая колонка - Распределение оценок -->
      <div class="distribution">
        {[5, 4, 3, 2, 1].map((rating) => {
          const count = ratingDistribution[rating as keyof typeof ratingDistribution] || 0;
          return (
            <div class="distribution-row">
              <span class="rating-label">{rating} ★</span>
              <div class="progress-bar">
                <div 
                  class="progress-fill" 
                  style={`width: ${getPercentage(count)}%`}
                ></div>
              </div>
              <span class="rating-count">{count}</span>
            </div>
          );
        })}
      </div>
    </div>
  </div>
</section>

<style>
  .voting-summary {
    background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
    border-radius: 1.5rem;
    padding: 3rem;
    margin: 3rem 0;
    border: 2px solid #d4af37;
  }

  .summary-container {
    max-width: 1200px;
    margin: 0 auto;
  }

  .summary-title {
    color: #1e293b;
    font-size: clamp(1.5rem, 3vw, 2rem);
    font-weight: 800;
    text-align: center;
    margin: 0 0 2.5rem 0;
    letter-spacing: -0.02em;
  }

  .summary-grid {
    display: grid;
    grid-template-columns: 1fr 1.5fr;
    gap: 3rem;
    align-items: center;
  }

  .overall-rating {
    text-align: center;
    padding: 2rem;
    background: rgba(255, 255, 255, 0.7);
    border-radius: 1rem;
    backdrop-filter: blur(10px);
  }

  .rating-big {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
  }

  .rating-number {
    font-size: 4rem;
    font-weight: 900;
    color: #d4af37;
    line-height: 1;
  }

  .rating-stars-big {
    display: flex;
    gap: 0.25rem;
    color: #d1d5db;
  }

  .star-icon {
    width: 2rem;
    height: 2rem;
    transition: color 0.2s ease;
  }

  .star-icon.filled {
    color: #fbbf24;
  }

  .total-votes-text,
  .total-reviews-text {
    color: #475569;
    font-size: 1rem;
    margin: 0.75rem 0 0 0;
  }

  .total-votes-text strong,
  .total-reviews-text strong {
    color: #1e293b;
    font-weight: 700;
  }

  .distribution {
    display: flex;
    flex-direction: column;
    gap: 1rem;
  }

  .distribution-row {
    display: flex;
    align-items: center;
    gap: 1rem;
  }

  .rating-label {
    min-width: 3rem;
    color: #475569;
    font-weight: 600;
    font-size: 0.95rem;
  }

  .progress-bar {
    flex: 1;
    height: 0.75rem;
    background: rgba(255, 255, 255, 0.5);
    border-radius: 1rem;
    overflow: hidden;
  }

  .progress-fill {
    height: 100%;
    background: linear-gradient(90deg, #d4af37 0%, #fbbf24 100%);
    border-radius: 1rem;
    transition: width 0.6s cubic-bezier(0.4, 0, 0.2, 1);
  }

  .rating-count {
    min-width: 2.5rem;
    text-align: right;
    color: #64748b;
    font-weight: 600;
    font-size: 0.875rem;
  }

  /* Анимации */
  .animate-on-scroll {
    opacity: 0;
    will-change: opacity, transform;
  }

  [data-animation="fade-up"] {
    transform: translateY(30px);
    transition: opacity 0.7s cubic-bezier(0.4, 0, 0.2, 1),
                transform 0.7s cubic-bezier(0.4, 0, 0.2, 1);
  }

  .animate-on-scroll.is-visible {
    opacity: 1;
    transform: translateY(0);
  }

  @media (prefers-reduced-motion: reduce) {
    .animate-on-scroll {
      opacity: 1;
      transform: none;
      transition: none;
    }
  }

  @media (max-width: 768px) {
    .voting-summary {
      padding: 2rem;
    }

    .summary-grid {
      grid-template-columns: 1fr;
      gap: 2rem;
    }

    .rating-number {
      font-size: 3rem;
    }

    .star-icon {
      width: 1.5rem;
      height: 1.5rem;
    }
  }
</style>
