"use client";

import { useEffect, useState } from "react";

type GalleryImage = {
  id: number;
  image: string;
};

type GalleryItem = {
  id: number;
  title: string;
  description: string | null;
  image: string;
  category: string | null;
  galleryimage: GalleryImage[];
};

const ITEMS_PER_PAGE = 3;

export default function Gallery() {
  const [gallery, setGallery] = useState<GalleryItem[]>([]);
  const [loading, setLoading] = useState(true);

  const [currentPage, setCurrentPage] = useState(1);

  const [activeImages, setActiveImages] = useState<
    Record<number, number>
  >({});

  const [lightbox, setLightbox] = useState<{
    galleryId: number;
    imageIndex: number;
  } | null>(null);

  const [pageReady, setPageReady] = useState(false);

  // =========================
  // PAGE ANIMATION
  // =========================

  useEffect(() => {
    const timer = setTimeout(() => {
      setPageReady(true);
    }, 80);

    return () => clearTimeout(timer);
  }, []);

  // =========================
  // GET GALLERY
  // =========================

  useEffect(() => {
    const getGallery = async () => {
      try {
        const response = await fetch("/api/gallery", {
          cache: "no-store",
        });

        const result = await response.json();

        if (result.success) {
          setGallery(result.data);
        }
      } catch (error) {
        console.error("GET GALLERY:", error);
      } finally {
        setLoading(false);
      }
    };

    getGallery();
  }, []);

  // =========================
  // GET IMAGES
  // =========================

  const getImages = (item: GalleryItem): string[] => {
    if (
      item.galleryimage &&
      item.galleryimage.length > 0
    ) {
      return item.galleryimage.map(
        (image) => image.image
      );
    }

    if (item.image) {
      return [item.image];
    }

    return [];
  };

  // =========================
  // CHANGE IMAGE
  // =========================

  const changeImage = (
    galleryId: number,
    direction: number,
    totalImages: number
  ) => {
    if (totalImages <= 1) return;

    const current = activeImages[galleryId] ?? 0;

    const next =
      (current + direction + totalImages) %
      totalImages;

    setActiveImages((previous) => ({
      ...previous,
      [galleryId]: next,
    }));
  };

  // =========================
  // SELECT IMAGE
  // =========================

  const selectImage = (
    galleryId: number,
    index: number
  ) => {
    setActiveImages((previous) => ({
      ...previous,
      [galleryId]: index,
    }));
  };

  // =========================
  // OPEN LIGHTBOX
  // =========================

  const openLightbox = (
    galleryId: number,
    imageIndex: number
  ) => {
    setLightbox({
      galleryId,
      imageIndex,
    });
  };

  // =========================
  // PAGINATION
  // =========================

  const totalPages = Math.ceil(
    gallery.length / ITEMS_PER_PAGE
  );

  const startIndex =
    (currentPage - 1) * ITEMS_PER_PAGE;

  const currentGallery = gallery.slice(
    startIndex,
    startIndex + ITEMS_PER_PAGE
  );

  // =========================
  // PAGE CHANGE
  // =========================

  const changePage = (page: number) => {
    setCurrentPage(page);

    setTimeout(() => {
      const section =
        document.getElementById("gallery");

      if (section) {
        window.scrollTo({
          top: section.offsetTop - 80,
          behavior: "smooth",
        });
      }
    }, 50);
  };

  // =========================
  // LOADING
  // =========================

  if (loading) {
    return (
      <>
        <style>{`
          .gallery-loading-section {
            position: relative;
            overflow: hidden;
          }

          .gallery-loading-card {
  width: 100%;
  max-width: 520px;
  margin: 0 auto;
  padding: 55px 30px;
  border-radius: 24px;
  background: #ffffff;
  border: 1px solid rgba(25, 135, 84, 0.22);
  box-shadow:
    0 7px 24px rgba(25, 135, 84, 0.055),
    0 2px 7px rgba(0, 0, 0, 0.035);
}

          .gallery-loading-icon {
            animation: galleryIconPulse 2s ease-in-out infinite;
          }

          @keyframes galleryIconPulse {
            0%,
            100% {
              transform: scale(1);
            }

            50% {
              transform: scale(1.06);
            }
          }

          .gallery-loading-line {
            position: relative;
            width: 180px;
            height: 4px;
            margin: 0 auto;
            overflow: hidden;
            border-radius: 999px;
            background: rgba(25, 135, 84, 0.08);
          }

          .gallery-loading-line span {
            position: absolute;
            top: 0;
            left: -40%;
            width: 40%;
            height: 100%;
            border-radius: 999px;
            background: linear-gradient(
              90deg,
              transparent,
              #198754,
              #42b883,
              #198754,
              transparent
            );
            animation: galleryLoadingMove 2.2s linear infinite;
          }

          @keyframes galleryLoadingMove {
            0% {
              left: -40%;
            }

            100% {
              left: 100%;
            }
          }
        `}</style>

        <section
          id="gallery"
          className="py-5 gallery-loading-section"
          style={{
            background:
              "linear-gradient(180deg, #ffffff 0%, #f8faf9 100%)",
          }}
        >
          <div className="container py-5">
            <div
              className="gallery-loading-card text-center"
              style={{
                minHeight: "400px",
                display: "flex",
                flexDirection: "column",
                justifyContent: "center",
                alignItems: "center",
              }}
            >
              <div
                className="gallery-loading-icon d-flex align-items-center justify-content-center rounded-circle"
                style={{
                  width: "76px",
                  height: "76px",
                  background:
                    "rgba(25,135,84,0.09)",
                  color: "#198754",
                }}
              >
                <i
                  className="bi bi-images"
                  style={{
                    fontSize: "2rem",
                  }}
                ></i>
              </div>

              <h5
                className="fw-bold mt-4 mb-2"
                style={{
                  color: "#17221c",
                }}
              >
                Loading Gallery
              </h5>

              <p className="text-secondary mb-4">
                Preparing gallery content...
              </p>

              <div className="gallery-loading-line">
                <span></span>
              </div>
            </div>
          </div>
        </section>
      </>
    );
  }

  return (
    <>
      <style>{`
        /* ==========================================
           GALLERY SECTION
        ========================================== */

        .gallery-section {
          position: relative;
          overflow: hidden;
        }

        /* ==========================================
           HEADER
        ========================================== */

        .gallery-header {
          position: relative;
        }

        .gallery-badge {
          display: inline-flex;
          align-items: center;
          gap: 8px;
          padding: 9px 15px;
          border-radius: 999px;
          background: rgba(25, 135, 84, 0.09);
          border: 1px solid rgba(25, 135, 84, 0.12);
          color: #198754;
          font-size: 0.76rem;
          font-weight: 700;
          letter-spacing: 0.09em;
          text-transform: uppercase;
        }

        .gallery-badge-dot {
          width: 7px;
          height: 7px;
          border-radius: 50%;
          background: #198754;
          box-shadow:
            0 0 0 5px rgba(25, 135, 84, 0.08);
          animation: galleryBadgePulse 2s ease-in-out infinite;
        }

        @keyframes galleryBadgePulse {
          0%,
          100% {
            transform: scale(1);
            opacity: 1;
          }

          50% {
            transform: scale(1.3);
            opacity: 0.65;
          }
        }

        .gallery-title-wrapper {
          display: inline-flex;
          flex-direction: column;
          align-items: center;
        }

        .gallery-title {
          color: #17221c;
          font-size: clamp(2.2rem, 5vw, 3.7rem);
          line-height: 1.08;
          letter-spacing: -0.04em;
        }

        .gallery-title-green {
          color: #198754;
        }

        .gallery-title-line {
          position: relative;
          width: 90px;
          height: 4px;
          margin-top: 16px;
          overflow: hidden;
          border-radius: 999px;
          background: rgba(25, 135, 84, 0.12);
        }

        .gallery-title-line::before {
          content: "";
          position: absolute;
          inset: 0;
          background: linear-gradient(
            90deg,
            transparent,
            rgba(25, 135, 84, 0.22),
            transparent
          );
        }

        .gallery-title-line span {
          position: absolute;
          top: 0;
          left: -40%;
          width: 40%;
          height: 100%;
          border-radius: 999px;
          background: linear-gradient(
            90deg,
            transparent,
            #198754,
            #42b883,
            #198754,
            transparent
          );
          box-shadow:
            0 0 8px rgba(25, 135, 84, 0.3);
          animation: galleryTitleMove 2.8s linear infinite;
        }

        @keyframes galleryTitleMove {
          0% {
            left: -40%;
          }

          100% {
            left: 100%;
          }
        }

        .gallery-description {
          max-width: 720px;
          color: #6c757d;
          font-size: 1rem;
          line-height: 1.8;
        }

        /* ==========================================
           GALLERY CARD
        ========================================== */

        .gallery-card {
  position: relative;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  height: 100%;
  background: #ffffff;
  border: 1px solid rgba(25, 135, 84, 0.24);
  border-radius: 24px;
  box-shadow:
    0 7px 24px rgba(25, 135, 84, 0.055),
    0 2px 7px rgba(0, 0, 0, 0.035);
  transition:
    transform 0.4s cubic-bezier(0.22, 1, 0.36, 1),
    box-shadow 0.4s ease,
    border-color 0.4s ease;
}

.gallery-card:hover {
  transform: translateY(-7px);
  border-color: rgba(25, 135, 84, 0.38);
  box-shadow:
    0 16px 36px rgba(25, 135, 84, 0.09),
    0 4px 12px rgba(0, 0, 0, 0.035);
}

        /* ==========================================
           GREEN LINE ABOVE CARD
        ========================================== */

        .gallery-card-line {
          position: relative;
          width: 100%;
          height: 5px;
          flex-shrink: 0;
          overflow: hidden;
          background: rgba(25, 135, 84, 0.08);
          z-index: 5;
        }

        .gallery-card-line::before {
          content: "";
          position: absolute;
          inset: 0;
          background: linear-gradient(
            90deg,
            rgba(25, 135, 84, 0.08),
            rgba(25, 135, 84, 0.2),
            rgba(25, 135, 84, 0.08)
          );
        }

        .gallery-card-line span {
          position: absolute;
          top: 0;
          left: -30%;
          width: 30%;
          height: 100%;
          border-radius: 999px;
          background: linear-gradient(
            90deg,
            transparent,
            #198754,
            #42b883,
            #198754,
            transparent
          );
          box-shadow:
            0 0 9px rgba(25, 135, 84, 0.35);
          animation: galleryCardLineMove 3.2s linear infinite;
        }

        @keyframes galleryCardLineMove {
          0% {
            left: -30%;
          }

          100% {
            left: 100%;
          }
        }

        /* ==========================================
           IMAGE
        ========================================== */

        .gallery-image-area {
          position: relative;
          height: 260px;
          flex-shrink: 0;
          overflow: hidden;
          background:
            linear-gradient(
              135deg,
              #edf5f0,
              #ffffff
            );
        }

        .gallery-main-image {
          display: block;
          width: 100%;
          height: 100%;
          object-fit: cover;
          cursor: zoom-in;
          transition:
            transform 0.7s cubic-bezier(0.22, 1, 0.36, 1),
            opacity 0.45s ease;
        }

        .gallery-card:hover .gallery-main-image {
          transform: scale(1.045);
        }

        .gallery-image-overlay {
          position: absolute;
          inset: 0;
          pointer-events: none;
          background:
            linear-gradient(
              180deg,
              rgba(0, 0, 0, 0.02) 15%,
              rgba(0, 0, 0, 0) 50%,
              rgba(0, 0, 0, 0.34) 100%
            );
        }

        /* ==========================================
           CATEGORY
        ========================================== */

        .gallery-image-category {
          position: absolute;
          top: 15px;
          left: 15px;
          z-index: 3;
        }

        .gallery-category-badge {
          display: inline-flex;
          align-items: center;
          padding: 8px 12px;
          border-radius: 999px;
          background: rgba(255, 255, 255, 0.94);
          color: #198754;
          font-size: 0.71rem;
          font-weight: 700;
          box-shadow:
            0 6px 20px rgba(0, 0, 0, 0.12);
          backdrop-filter: blur(10px);
        }

        /* ==========================================
           COUNTER
        ========================================== */

        .gallery-image-counter {
          position: absolute;
          right: 15px;
          bottom: 15px;
          z-index: 3;
        }

        .gallery-counter-badge {
          display: inline-flex;
          align-items: center;
          padding: 8px 12px;
          border-radius: 999px;
          background: rgba(20, 30, 25, 0.72);
          color: #ffffff;
          font-size: 0.71rem;
          font-weight: 700;
          backdrop-filter: blur(10px);
        }

        /* ==========================================
           NAVIGATION
        ========================================== */

        .gallery-nav-button {
          position: absolute;
          top: 50%;
          z-index: 5;
          display: flex;
          align-items: center;
          justify-content: center;
          width: 40px;
          height: 40px;
          padding: 0;
          border: none;
          border-radius: 50%;
          background: rgba(255, 255, 255, 0.94);
          color: #198754;
          box-shadow:
            0 7px 20px rgba(0, 0, 0, 0.14);
          transform: translateY(-50%);
          transition:
            transform 0.25s ease,
            background 0.25s ease,
            box-shadow 0.25s ease;
        }

        .gallery-nav-button:hover {
          background: #ffffff;
          color: #198754;
          box-shadow:
            0 10px 25px rgba(0, 0, 0, 0.18);
          transform: translateY(-50%) scale(1.08);
        }

        .gallery-nav-prev {
          left: 12px;
        }

        .gallery-nav-next {
          right: 12px;
        }

        /* ==========================================
           DOTS
        ========================================== */

        .gallery-dots-wrapper {
          display: flex;
          align-items: center;
          justify-content: center;
          min-height: 28px;
          padding-top: 10px;
        }

        .gallery-dot {
          width: 7px;
          height: 7px;
          padding: 0;
          border: 0;
          border-radius: 999px;
          background: #d4ddd8;
          cursor: pointer;
          transition:
            width 0.3s ease,
            background 0.3s ease,
            transform 0.3s ease;
        }

        .gallery-dot:hover {
          transform: scale(1.15);
        }

        .gallery-dot.active {
          width: 22px;
          background: #198754;
        }

        /* ==========================================
           CONTENT
        ========================================== */

        .gallery-card-content {
          display: flex;
          flex-direction: column;
          flex: 1;
          padding: 25px;
        }

        .gallery-content-category {
          display: inline-flex;
          align-items: center;
          gap: 5px;
          margin-bottom: 10px;
          color: #198754;
          font-size: 0.75rem;
          font-weight: 700;
          letter-spacing: 0.02em;
        }

        .gallery-card-title {
          margin-bottom: 13px;
          color: #17221c;
          font-size: 1.22rem;
          line-height: 1.4;
          letter-spacing: -0.015em;
        }

        .gallery-small-line {
          width: 42px;
          height: 3px;
          margin-bottom: 15px;
          border-radius: 999px;
          background: linear-gradient(
            90deg,
            #198754,
            #42b883
          );
        }

        /* ==========================================
           FULL DESCRIPTION
        ========================================== */

        .gallery-card-description {
          margin: 0;
          color: #6c757d;
          font-size: 0.9rem;
          line-height: 1.85;
          text-align: justify;
          white-space: normal;
          overflow-wrap: break-word;
          word-break: normal;
        }

        /* ==========================================
           EMPTY STATE
        ========================================== */

        .gallery-empty-card {
  position: relative;
  max-width: 520px;
  margin: 0 auto;
  overflow: hidden;
  padding: 55px 30px;
  border: 1px solid rgba(25, 135, 84, 0.22);
  border-radius: 24px;
  background: rgba(255, 255, 255, 0.96);
  box-shadow:
    0 7px 24px rgba(25, 135, 84, 0.055),
    0 2px 7px rgba(0, 0, 0, 0.035);
  transition:
    border-color 0.3s ease,
    box-shadow 0.3s ease;
}

.gallery-empty-card:hover {
  border-color: rgba(25, 135, 84, 0.30);
  box-shadow:
    0 12px 30px rgba(25, 135, 84, 0.075),
    0 3px 10px rgba(0, 0, 0, 0.035);
}

        .gallery-empty-card::before {
          content: "";
          position: absolute;
          top: 0;
          left: -30%;
          width: 30%;
          height: 4px;
          background: linear-gradient(
            90deg,
            transparent,
            #198754,
            #42b883,
            transparent
          );
          animation:
            galleryCardLineMove 3s linear infinite;
        }

        .gallery-empty-icon {
          display: flex;
          align-items: center;
          justify-content: center;
          width: 78px;
          height: 78px;
          margin: 0 auto;
          border-radius: 20px;
          background: rgba(25, 135, 84, 0.08);
          color: #198754;
        }

        /* ==========================================
           PAGINATION
        ========================================== */

        .gallery-pagination {
          margin-top: 50px;
        }

        .gallery-page-button {
          display: flex;
          align-items: center;
          justify-content: center;
          min-width: 42px;
          height: 42px;
          padding: 0 15px;
          border: 1px solid rgba(25, 135, 84, 0.22);
          border-radius: 999px;
          background: #ffffff;
          color: #198754;
          font-size: 0.84rem;
          font-weight: 700;
          transition:
            transform 0.25s ease,
            background 0.25s ease,
            color 0.25s ease,
            box-shadow 0.25s ease,
            border-color 0.25s ease;
        }

        .gallery-page-button:hover:not(:disabled) {
          border-color: #198754;
          background: #f1faf5;
          color: #198754;
          transform: translateY(-2px);
          box-shadow:
            0 7px 18px rgba(25, 135, 84, 0.12);
        }

        .gallery-page-button.active {
          border-color: #198754;
          background: #198754;
          color: #ffffff;
          box-shadow:
            0 8px 20px rgba(25, 135, 84, 0.2);
        }

        .gallery-page-button:disabled {
          cursor: not-allowed;
          opacity: 0.45;
        }

        .gallery-page-number {
          width: 42px;
          min-width: 42px;
          padding: 0;
          border-radius: 50%;
        }

        /* ==========================================
           FOOTER
        ========================================== */

        .gallery-footer {
          color: #6c757d;
          font-size: 0.84rem;
        }

        /* ==========================================
           LIGHTBOX
        ========================================== */

        .gallery-lightbox {
          position: fixed;
          inset: 0;
          z-index: 9999;
          display: flex;
          align-items: center;
          justify-content: center;
          padding: 30px;
          background: rgba(0, 0, 0, 0.9);
          backdrop-filter: blur(5px);
          animation: galleryLightboxIn 0.25s ease;
        }

        @keyframes galleryLightboxIn {
          from {
            opacity: 0;
          }

          to {
            opacity: 1;
          }
        }

        .gallery-lightbox-image {
          max-width: 90%;
          max-height: 84vh;
          object-fit: contain;
          border-radius: 14px;
          box-shadow:
            0 20px 70px rgba(0, 0, 0, 0.35);
          animation: galleryImageIn 0.3s ease;
        }

        @keyframes galleryImageIn {
          from {
            opacity: 0;
            transform: scale(0.97);
          }

          to {
            opacity: 1;
            transform: scale(1);
          }
        }

        .gallery-lightbox-close {
          position: absolute;
          top: 22px;
          right: 22px;
          z-index: 10;
          display: flex;
          align-items: center;
          justify-content: center;
          width: 44px;
          height: 44px;
          border: none;
          border-radius: 50%;
          background: rgba(255, 255, 255, 0.92);
          color: #17221c;
          transition:
            transform 0.25s ease,
            background 0.25s ease;
        }

        .gallery-lightbox-close:hover {
          background: #ffffff;
          transform: scale(1.07);
        }

        .gallery-lightbox-nav {
          position: absolute;
          top: 50%;
          z-index: 10;
          display: flex;
          align-items: center;
          justify-content: center;
          width: 46px;
          height: 46px;
          border: none;
          border-radius: 50%;
          background: rgba(255, 255, 255, 0.92);
          color: #198754;
          transform: translateY(-50%);
          transition:
            transform 0.25s ease,
            background 0.25s ease;
        }

        .gallery-lightbox-nav:hover {
          background: #ffffff;
          transform: translateY(-50%) scale(1.07);
        }

        .gallery-lightbox-prev {
          left: 22px;
        }

        .gallery-lightbox-next {
          right: 22px;
        }

        .gallery-lightbox-counter {
          position: absolute;
          bottom: 22px;
          left: 50%;
          z-index: 10;
          padding: 7px 13px;
          border-radius: 999px;
          background: rgba(0, 0, 0, 0.65);
          color: #ffffff;
          font-size: 0.78rem;
          font-weight: 700;
          transform: translateX(-50%);
        }

        /* ==========================================
           RESPONSIVE
        ========================================== */

        @media (max-width: 767.98px) {
          .gallery-title-line {
            width: 78px;
            margin-top: 14px;
          }

          .gallery-card {
            border-radius: 18px;
          }

          .gallery-card-content {
            padding: 22px;
          }

          .gallery-image-area {
            height: 245px;
          }

          .gallery-pagination {
            margin-top: 40px;
          }

          .gallery-lightbox {
            padding: 20px;
          }

          .gallery-lightbox-image {
            max-width: 94%;
            max-height: 78vh;
          }

          .gallery-lightbox-prev {
            left: 10px;
          }

          .gallery-lightbox-next {
            right: 10px;
          }
        }

        @media (max-width: 575.98px) {
          .gallery-title-line {
            width: 70px;
            height: 3px;
          }

          .gallery-card {
            border-radius: 18px;
          }

          .gallery-card-line {
            height: 4px;
          }

          .gallery-image-area {
            height: 230px;
          }

          .gallery-card-content {
            padding: 20px;
          }

          .gallery-nav-button {
            width: 37px;
            height: 37px;
          }

          .gallery-page-button {
            min-width: 40px;
            height: 40px;
            padding: 0 12px;
          }

          .gallery-page-number {
            width: 40px;
            min-width: 40px;
          }

          .gallery-lightbox-close {
            top: 15px;
            right: 15px;
          }

          .gallery-lightbox-counter {
            bottom: 15px;
          }
        }
      `}</style>

      <section
        id="gallery"
        className="py-5 gallery-section"
        style={{
          background:
            "radial-gradient(circle at 10% 10%, rgba(25,135,84,0.06), transparent 32%), radial-gradient(circle at 90% 30%, rgba(13,110,253,0.04), transparent 30%), linear-gradient(180deg, #ffffff 0%, #f8faf9 50%, #ffffff 100%)",
        }}
      >
        <div className="container py-5">

          {/* =========================================
              HEADER
          ========================================= */}

          <div
            className="text-center mb-5 pb-lg-3 gallery-header"
            style={{
              opacity: pageReady ? 1 : 0,
              transform: pageReady
                ? "translateY(0)"
                : "translateY(20px)",
              transition:
                "opacity .8s ease, transform .8s ease",
            }}
          >
            <div className="mb-3">
              <span className="gallery-badge">
                <span className="gallery-badge-dot"></span>
                <i className="bi bi-images"></i>
                Gallery
              </span>
            </div>

            <div className="gallery-title-wrapper">
              <h2 className="fw-bold mb-0 text-center gallery-title">
                Documentation &{" "}
                <span className="gallery-title-green">
                  Gallery
                </span>
              </h2>

              <div className="gallery-title-line">
                <span></span>
              </div>
            </div>

            <p className="mx-auto mb-0 mt-3 text-center gallery-description">
              Documentation of activities, projects,
              workshops, and various professional
              activities.
            </p>
          </div>

          {/* =========================================
              EMPTY STATE
          ========================================= */}

          {gallery.length === 0 && (
            <div
              className="gallery-empty-card text-center"
              style={{
                opacity: pageReady ? 1 : 0,
                transform: pageReady
                  ? "translateY(0)"
                  : "translateY(20px)",
                transition:
                  "opacity .8s ease .1s, transform .8s ease .1s",
              }}
            >
              <div className="gallery-empty-icon">
                <i
                  className="bi bi-images"
                  style={{
                    fontSize: "2.2rem",
                  }}
                ></i>
              </div>

              <h5
                className="fw-bold mt-4 mb-2"
                style={{
                  color: "#17221c",
                }}
              >
                No Gallery Yet
              </h5>

              <p className="text-secondary mb-0">
                Gallery data will be displayed here
                after being added.
              </p>
            </div>
          )}

          {/* =========================================
              GALLERY GRID
          ========================================= */}

          {gallery.length > 0 && (
            <div className="row g-4">
              {currentGallery.map((item, itemIndex) => {
                const images = getImages(item);

                const activeIndex =
                  activeImages[item.id] ?? 0;

                const currentImage =
                  images[activeIndex];

                return (
                  <div
                    key={item.id}
                    className="col-12 col-md-6 col-lg-4"
                    style={{
                      opacity: pageReady ? 1 : 0,
                      transform: pageReady
                        ? "translateY(0)"
                        : "translateY(35px)",
                      transition: `
                        opacity .7s ease ${
                          0.1 + itemIndex * 0.1
                        }s,
                        transform .7s ease ${
                          0.1 + itemIndex * 0.1
                        }s
                      `,
                    }}
                  >
                    <article className="gallery-card">

                      {/* =================================
                          GREEN LINE ABOVE CARD
                      ================================= */}

                      <div className="gallery-card-line">
                        <span></span>
                      </div>

                      {/* =================================
                          IMAGE
                      ================================= */}

                      <div className="gallery-image-area">
                        {currentImage ? (
                          <>
                            <img
                              key={currentImage}
                              src={currentImage}
                              alt={`${item.title} - Gambar ${
                                activeIndex + 1
                              }`}
                              onClick={() =>
                                openLightbox(
                                  item.id,
                                  activeIndex
                                )
                              }
                              className="gallery-main-image"
                            />

                            <div className="gallery-image-overlay"></div>

                            {item.category && (
                              <div className="gallery-image-category">
                                <span className="gallery-category-badge">
                                  <i className="bi bi-tag me-1"></i>
                                  {item.category}
                                </span>
                              </div>
                            )}

                            {images.length > 1 && (
                              <div className="gallery-image-counter">
                                <span className="gallery-counter-badge">
                                  <i className="bi bi-images me-1"></i>
                                  {activeIndex + 1} /{" "}
                                  {images.length}
                                </span>
                              </div>
                            )}

                            {images.length > 1 && (
                              <button
                                type="button"
                                className="gallery-nav-button gallery-nav-prev"
                                onClick={() =>
                                  changeImage(
                                    item.id,
                                    -1,
                                    images.length
                                  )
                                }
                                aria-label="Gambar sebelumnya"
                              >
                                <i className="bi bi-chevron-left"></i>
                              </button>
                            )}

                            {images.length > 1 && (
                              <button
                                type="button"
                                className="gallery-nav-button gallery-nav-next"
                                onClick={() =>
                                  changeImage(
                                    item.id,
                                    1,
                                    images.length
                                  )
                                }
                                aria-label="Gambar berikutnya"
                              >
                                <i className="bi bi-chevron-right"></i>
                              </button>
                            )}
                          </>
                        ) : (
                          <div className="w-100 h-100 d-flex align-items-center justify-content-center">
                            <div
                              className="d-flex align-items-center justify-content-center rounded-circle"
                              style={{
                                width: "82px",
                                height: "82px",
                                background:
                                  "rgba(25,135,84,0.09)",
                                color: "#198754",
                              }}
                            >
                              <i
                                className="bi bi-images"
                                style={{
                                  fontSize: "2.3rem",
                                }}
                              ></i>
                            </div>
                          </div>
                        )}
                      </div>

                      {/* =================================
                          IMAGE DOTS
                      ================================= */}

                      {images.length > 1 && (
                        <div className="gallery-dots-wrapper">
                          <div className="d-flex justify-content-center align-items-center gap-2">
                            {images.map(
                              (_, index) => {
                                const active =
                                  index ===
                                  activeIndex;

                                return (
                                  <button
                                    key={index}
                                    type="button"
                                    onClick={() =>
                                      selectImage(
                                        item.id,
                                        index
                                      )
                                    }
                                    className={`gallery-dot ${
                                      active
                                        ? "active"
                                        : ""
                                    }`}
                                    aria-label={`Tampilkan gambar ${
                                      index + 1
                                    }`}
                                  />
                                );
                              }
                            )}
                          </div>
                        </div>
                      )}

                      {/* =================================
                          CONTENT
                      ================================= */}

                      <div className="gallery-card-content">

                        {item.category && (
                          <div className="gallery-content-category">
                            <i className="bi bi-folder2-open"></i>
                            <span>
                              {item.category}
                            </span>
                          </div>
                        )}

                        <h4 className="fw-bold gallery-card-title">
                          {item.title}
                        </h4>

                        <div className="gallery-small-line"></div>

                        {item.description && (
                          <p className="gallery-card-description">
                            {item.description}
                          </p>
                        )}
                      </div>
                    </article>
                  </div>
                );
              })}
            </div>
          )}

          {/* =========================================
              PAGINATION
          ========================================= */}

          {totalPages > 1 && (
            <div
              className="d-flex justify-content-center align-items-center flex-wrap gap-2 gallery-pagination"
              style={{
                opacity: pageReady ? 1 : 0,
                transition:
                  "opacity 1s ease .5s",
              }}
            >
              <button
                type="button"
                className="gallery-page-button"
                disabled={currentPage === 1}
                onClick={() =>
                  changePage(currentPage - 1)
                }
              >
                <i className="bi bi-chevron-left me-1"></i>

                <span className="d-none d-sm-inline">
                  Previous
                </span>
              </button>

              <div className="d-flex gap-2">
                {Array.from(
                  {
                    length: totalPages,
                  },
                  (_, index) => {
                    const page = index + 1;

                    return (
                      <button
                        key={page}
                        type="button"
                        className={`gallery-page-button gallery-page-number ${
                          currentPage === page
                            ? "active"
                            : ""
                        }`}
                        onClick={() =>
                          changePage(page)
                        }
                        aria-label={`Halaman ${page}`}
                        aria-current={
                          currentPage === page
                            ? "page"
                            : undefined
                        }
                      >
                        {page}
                      </button>
                    );
                  }
                )}
              </div>

              <button
                type="button"
                className="gallery-page-button"
                disabled={
                  currentPage === totalPages
                }
                onClick={() =>
                  changePage(currentPage + 1)
                }
              >
                <span className="d-none d-sm-inline">
                  Next
                </span>

                <i className="bi bi-chevron-right ms-1"></i>
              </button>
            </div>
          )}

          {/* =========================================
              FOOTER
          ========================================= */}

          {gallery.length > 0 && (
            <div
              className="text-center mt-5 pt-4 gallery-footer"
              style={{
                opacity: pageReady ? 1 : 0,
                transition:
                  "opacity 1s ease .6s",
              }}
            >
              <div className="d-inline-flex align-items-center gap-2">
                <i className="bi bi-images text-success"></i>

                <span>
                  Professional activities and
                  documentation
                </span>
              </div>
            </div>
          )}
        </div>
      </section>

      {/* =========================================
          LIGHTBOX
      ========================================= */}

      {lightbox && (
        <div
          className="gallery-lightbox"
          onClick={() => setLightbox(null)}
        >
          <button
            type="button"
            className="gallery-lightbox-close"
            onClick={() =>
              setLightbox(null)
            }
            aria-label="Tutup"
          >
            <i className="bi bi-x-lg"></i>
          </button>

          {(() => {
            const item = gallery.find(
              (galleryItem) =>
                galleryItem.id ===
                lightbox.galleryId
            );

            if (!item) return null;

            const images = getImages(item);

            const currentLightboxImage =
              images[lightbox.imageIndex];

            return (
              <>
                <img
                  key={currentLightboxImage}
                  src={currentLightboxImage}
                  alt={`${item.title} - Gallery image`}
                  className="gallery-lightbox-image"
                  onClick={(event) =>
                    event.stopPropagation()
                  }
                />

                {images.length > 1 && (
                  <button
                    type="button"
                    className="gallery-lightbox-nav gallery-lightbox-prev"
                    onClick={(event) => {
                      event.stopPropagation();

                      setLightbox({
                        galleryId:
                          lightbox.galleryId,
                        imageIndex:
                          (lightbox.imageIndex -
                            1 +
                            images.length) %
                          images.length,
                      });
                    }}
                    aria-label="Gambar sebelumnya"
                  >
                    <i className="bi bi-chevron-left"></i>
                  </button>
                )}

                {images.length > 1 && (
                  <button
                    type="button"
                    className="gallery-lightbox-nav gallery-lightbox-next"
                    onClick={(event) => {
                      event.stopPropagation();

                      setLightbox({
                        galleryId:
                          lightbox.galleryId,
                        imageIndex:
                          (lightbox.imageIndex +
                            1) %
                          images.length,
                      });
                    }}
                    aria-label="Gambar berikutnya"
                  >
                    <i className="bi bi-chevron-right"></i>
                  </button>
                )}

                <span className="gallery-lightbox-counter">
                  {lightbox.imageIndex + 1} /{" "}
                  {images.length}
                </span>
              </>
            );
          })()}
        </div>
      )}
    </>
  );
}