---
import { sanityClient } from "sanity:client";

const documents = await sanityClient.fetch(`*[_type == "documents"]{
  title,
  description,
  "fileUrl": file.asset->url,
  "iconUrl": icon.asset->url
}`);

// Проверяем наличие данных и добавляем fallback
const documentFiles = documents && documents.length > 0 ? documents : [];

// Данные для заголовка секции
const sectionData = {
  title: "ДОКУМЕНТЫ",
  description: "Здесь размещены наши документы и сертификаты"
};
---

<section class="bg-gradient-to-br from-gray-800 via-gray-900 to-black text-white py-20 px-8 lg:px-24 relative overflow-hidden">
  <!-- Декоративные элементы -->
  <div class="absolute inset-0 opacity-5">
    <div class="absolute top-20 left-20 w-32 h-32 bg-yellow-500 rounded-full blur-3xl"></div>
    <div class="absolute bottom-20 right-20 w-40 h-40 bg-yellow-400 rounded-full blur-3xl"></div>
    <div class="absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 w-60 h-60 bg-yellow-300 rounded-full blur-3xl"></div>
  </div>

  <div class="relative z-10 max-w-7xl mx-auto">
    <div class="text-center mb-16">
      <h2 class="text-6xl lg:text-8xl font-bold text-yellow-500 mb-6 font-nevduplenysh">
        {sectionData.title}
      </h2>
      <p class="text-xl lg:text-2xl text-gray-300 max-w-4xl mx-auto font-gilroy leading-relaxed">
        {sectionData.description}
      </p>
    </div>

    {documentFiles.length > 0 ? (
      <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8">
        {
          documentFiles.map((doc, index) => (
            <div class="group relative bg-gradient-to-br from-gray-800 to-gray-900 rounded-2xl p-8 border border-gray-700 hover:border-yellow-500/50 transition-all duration-500 hover:transform hover:scale-105">
              <div class="absolute inset-0 bg-gradient-to-br from-yellow-500/10 to-transparent rounded-2xl opacity-0 group-hover:opacity-100 transition-opacity duration-500"></div>
              
              <div class="relative z-10">
                <!-- Иконка документа -->
                <div class="w-16 h-16 bg-gradient-to-br from-yellow-500 to-yellow-600 rounded-xl flex items-center justify-center mb-6 group-hover:shadow-lg group-hover:shadow-yellow-500/25 transition-shadow duration-500">
                  <svg class="w-8 h-8 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path>
                  </svg>
                </div>

                <!-- Заголовок документа -->
                <h3 class="text-2xl font-bold text-white mb-4 font-gilroy leading-tight">
                  {doc.title || "Документ"}
                </h3>

                <!-- Описание -->
                <p class="text-gray-300 mb-6 font-gilroy leading-relaxed">
                  {doc.description || "Описание документа"}
                </p>

                <!-- Информация о файле -->
                <div class="flex items-center justify-between mb-6 text-sm text-gray-400 font-gilroy">
                  <span class="bg-gray-700 px-3 py-1 rounded-lg">
                    PDF
                  </span>
                  <span>Документ</span>
                </div>

                <!-- Кнопка скачивания -->
                <a 
                  href={doc.fileUrl || "#"}
                  download
                  class="group/btn inline-flex items-center w-full justify-center px-6 py-4 bg-gradient-to-r from-yellow-500 to-yellow-600 text-black rounded-xl hover:from-yellow-400 hover:to-yellow-500 transition-all duration-300 transform hover:scale-105 font-gilroy font-semibold shadow-lg shadow-yellow-500/25"
                >
                  <svg class="w-5 h-5 mr-3 group-hover/btn:translate-y-1 transition-transform duration-300" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path>
                  </svg>
                  Скачать документ
                </a>
              </div>
            </div>
          ))
        }
      </div>
    ) : (
      <!-- Заглушка когда нет документов -->
      <div class="text-center py-16">
        <div class="w-24 h-24 bg-gradient-to-br from-gray-700 to-gray-800 rounded-2xl flex items-center justify-center mx-auto mb-8">
          <svg class="w-12 h-12 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path>
          </svg>
        </div>
        <h3 class="text-2xl font-bold text-gray-300 mb-4 font-gilroy">
          Документы скоро будут добавлены
        </h3>
        <p class="text-gray-400 font-gilroy">
          Мы работаем над наполнением этого раздела. Следите за обновлениями!
        </p>
      </div>
    )}

    <!-- Дополнительная информация -->
    <div class="mt-16 text-center">
      <div class="inline-flex items-center px-8 py-4 bg-gradient-to-r from-blue-500/20 to-blue-600/20 rounded-2xl border border-blue-500/30">
        <svg class="w-6 h-6 text-blue-400 mr-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
          <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m5.618-4.016A11.955 11.955 0 0112 2.944a11.955 11.955 0 01-8.618 3.04A12.02 12.02 0 003 9c0 5.591 3.824 10.29 9 11.622 5.176-1.332 9-6.03 9-11.622 0-1.042-.133-2.052-.382-3.016z"></path>
        </svg>
        <span class="text-blue-300 font-gilroy text-lg">Вся продукция сертифицирована и соответствует государственным стандартам</span>
      </div>
    </div>
  </div>
</section>
