---
export interface Review {
  id: string;
  name: string;
  role: string;
  avatar?: string;
  rating: number;
  date: string;
  text: string;
  caseType: string;
  verified: boolean;
}

interface Props {
  review: Review;
  category?: string;
}

const { review, category } = Astro.props;

// Функция генерации цвета аватарки по имени
function getAvatarColor(name: string): string {
  const colors = [
    "rgba(59, 130, 246, 0.8)",
    "rgba(16, 185, 129, 0.8)",
    "rgba(245, 158, 11, 0.8)",
    "rgba(239, 68, 68, 0.8)",
    "rgba(139, 92, 246, 0.8)",
    "rgba(236, 72, 153, 0.8)",
    "rgba(14, 165, 233, 0.8)",
    "rgba(34, 197, 94, 0.8)",
    "rgba(249, 115, 22, 0.8)",
    "rgba(99, 102, 241, 0.8)",
  ];

  // Генерируем индекс на основе имени
  let hash = 0;
  for (let i = 0; i < name.length; i++) {
    hash = name.charCodeAt(i) + ((hash << 5) - hash);
  }
  const index = Math.abs(hash) % colors.length;
  return colors[index];
}
---

<div data-category={review.caseType} class="review-card group">
  <!-- Внутренний контейнер для изоляции стилей -->
  <div class="card-content">
    <!-- Шапка -->
    <div class="flex items-start gap-4 mb-5">
      <div
        class="avatar-wrapper"
        style="background-color: {getAvatarColor(review.name)};"
      >
        {review.name.charAt(0).toUpperCase()}
      </div>

      <div class="flex-1 min-w-0 pt-1">
        <div class="flex items-start justify-between gap-2">
          <div class="min-w-0 flex-1">
            <h3 class="name">{review.name}</h3>
            <p class="role">{review.role}</p>
          </div>

          <!-- Бейдж "Проверено" всегда показываем, т.к. отзывы публикуются сразу -->
          <div class="verified-badge" title="Отзыв проверен">
            <svg class="w-3 h-3" fill="currentColor" viewBox="0 0 20 20">
              <path
                fill-rule="evenodd"
                d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z"
                clip-rule="evenodd"
              />
            </svg>
            <span>Проверено</span>
          </div>
        </div>

        <!-- Рейтинг -->
        <div class="flex items-center gap-1 mt-2">
          {
            [...Array(5)].map((_, i) => (
              <svg
                class={`w-4 h-4 transition-colors duration-300 ${i < review.rating ? "text-[var(--color-gold)]" : "text-[var(--color-gray-600)]"}`}
                fill="currentColor"
                viewBox="0 0 20 20"
              >
                <path d="M9.049 2.927c.3-.921 1.603-.921 1.902 0l1.07 3.292a1 1 0 00.95.69h3.462c.969 0 1.371 1.24.588 1.81l-2.8 2.034a1 1 0 00-.364 1.118l1.07 3.292c.3.921-.755 1.688-1.54 1.118l-2.8-2.034a1 1 0 00-1.175 0l-2.8 2.034c-.784.57-1.838-.197-1.539-1.118l1.07-3.292a1 1 0 00-.364-1.118L2.98 8.72c-.783-.57-.38-1.81.588-1.81h3.461a1 1 0 00.951-.69l1.07-3.292z" />
              </svg>
            ))
          }
        </div>
      </div>
    </div>

    <!-- Тип дела -->
    <div class="mb-4">
      <span class="case-tag">
        {review.caseType}
      </span>
    </div>

    <!-- Текст -->
    <blockquote class="review-text">
      {review.text}
    </blockquote>

    <!-- Футер -->
    <div class="card-footer">
      <span class="date">{review.date}</span>
      <div class="status">
        <svg
          class="w-4 h-4"
          fill="none"
          stroke="currentColor"
          viewBox="0 0 24 24"
        >
          <path
            stroke-linecap="round"
            stroke-linejoin="round"
            stroke-width="2"
            d="M5 13l4 4L19 7"></path>
        </svg>
        <span>Дело выиграно</span>
      </div>
    </div>
  </div>
</div>

<style>
  /* 1. Основной контейнер карточки */
  .review-card {
    position: relative;
    height: 100%;
    opacity: 1;
    transform: translateY(0) scale(1);
    transition:
      transform 0.5s cubic-bezier(0.2, 0.8, 0.2, 1),
      opacity 0.5s ease;
    will-change: transform, opacity;
  }

  /* Внутренняя обертка с фоном и границами */
  .card-content {
    height: 100%;
    padding: 1.5rem;
    background-color: var(--color-navy-dark);
    border: 1px solid rgba(75, 85, 99, 0.2);
    border-radius: 1.25rem;
    position: relative;
    overflow: hidden;
    transition:
      border-color 0.4s ease,
      box-shadow 0.4s ease,
      background-color 0.4s ease;
  }

  /* Эффект при наведении на ВСЮ карточку */
  .review-card:hover .card-content {
    border-color: rgba(212, 175, 55, 0.3);
    background-color: rgba(255, 255, 255, 0.01);
    box-shadow:
      0 20px 25px -5px rgba(0, 0, 0, 0.2),
      0 0 20px 0px rgba(212, 175, 55, 0.05);
  }

  .review-card:hover {
    transform: translateY(-8px) scale(1.02);
  }

  /* 2. Аватарка */
  .avatar-wrapper {
    width: 3.5rem;
    height: 3.5rem;
    border-radius: 1rem;
    border: 1px solid rgba(75, 85, 99, 0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--color-white);
    flex-shrink: 0;
    transition: transform 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
    text-transform: uppercase;
  }

  .review-card:hover .avatar-wrapper {
    transform: scale(1.1) rotate(5deg);
    border-color: rgba(212, 175, 55, 0.4);
  }

  /* 3. Текстовые стили */
  .name {
    font-weight: 700;
    color: var(--color-white);
    font-size: 1.125rem;
    line-height: 1.25;
    margin-bottom: 0.25rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .role {
    color: var(--color-gray-500);
    font-size: 0.875rem;
  }

  .case-tag {
    display: inline-block;
    padding: 0.25rem 0.75rem;
    background-color: rgba(212, 175, 55, 0.1);
    border: 1px solid rgba(212, 175, 55, 0.2);
    border-radius: 9999px;
    color: var(--color-gold);
    font-size: 0.75rem;
    font-weight: 600;
  }

  .review-text {
    color: var(--color-gray-400);
    font-size: 0.875rem;
    line-height: 1.625;
    margin-bottom: 1rem;
  }

  /* 4. Бейдж проверки */
  .verified-badge {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    padding: 0.25rem 0.5rem;
    background-color: rgba(16, 185, 129, 0.1);
    border: 1px solid rgba(16, 185, 129, 0.2);
    border-radius: 9999px;
    color: var(--color-emerald-400);
    flex-shrink: 0;
  }

  .verified-badge span {
    font-size: 10px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
  }

  @media (max-width: 640px) {
    .verified-badge span {
      display: none;
    }
  }

  /* 5. Футер */
  .card-footer {
    padding-top: 1rem;
    border-top: 1px solid rgba(75, 85, 99, 0.1);
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .date {
    font-size: 0.75rem;
    color: var(--color-gray-600);
  }

  .status {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    color: var(--color-emerald-400);
    font-size: 0.75rem;
    font-weight: 500;
  }

  /* 6. Состояния для фильтрации (JS) */
  .review-card.hidden {
    display: none;
  }

  .review-card.opacity-0 {
    opacity: 0;
    transform: scale(0.95) translateY(10px);
    pointer-events: none;
  }
</style>
