"use client";

import { useEffect, useState } from "react";

type CertificationImage = {
  id: number;
  image: string;
  certificationId: number;
  createdAt: string;
};

type Certification = {
  id: number;
  name: string;
  issuer: string;
  credentialId: string | null;
  issueDate: string;
  expiryDate: string | null;
  image: string | null;
  description: string | null;
  certificationimage: CertificationImage[];
};

export default function Certification() {
  const [certifications, setCertifications] = useState<
    Certification[]
  >([]);

  const [loading, setLoading] = useState(true);

  const [activeImages, setActiveImages] = useState<
    Record<number, number>
  >({});

  const [lightbox, setLightbox] = useState<{
    certificationId: number;
    imageIndex: number;
  } | null>(null);

  const [pageReady, setPageReady] = useState(false);

  // ============================================================
  // PAGE ANIMATION
  // ============================================================

  useEffect(() => {
    const timer = setTimeout(() => {
      setPageReady(true);
    }, 80);

    return () => clearTimeout(timer);
  }, []);

  // ============================================================
  // GET CERTIFICATIONS
  // ============================================================

  useEffect(() => {
    const getCertifications = async () => {
      try {
        const response = await fetch(
          "/api/certifications",
          {
            cache: "no-store",
          }
        );

        const result = await response.json();

        if (result.success) {
          setCertifications(result.data);
        }
      } catch (error) {
        console.error(
          "GET CERTIFICATIONS:",
          error
        );
      } finally {
        setLoading(false);
      }
    };

    getCertifications();
  }, []);

  // ============================================================
  // GET IMAGES
  // ============================================================

  const getImages = (
    certification: Certification
  ): CertificationImage[] => {
    if (
      certification.certificationimage &&
      certification.certificationimage.length > 0
    ) {
      return certification.certificationimage;
    }

    if (certification.image) {
      return [
        {
          id: 0,
          image: certification.image,
          certificationId: certification.id,
          createdAt: "",
        },
      ];
    }

    return [];
  };

  // ============================================================
  // FORMAT DATE
  // ============================================================

  const formatDate = (date: string) => {
    return new Date(date).toLocaleDateString(
      "id-ID",
      {
        day: "2-digit",
        month: "long",
        year: "numeric",
      }
    );
  };

  // ============================================================
  // CHANGE IMAGE
  // ============================================================

  const changeImage = (
    certificationId: number,
    direction: number,
    totalImages: number
  ) => {
    if (totalImages <= 1) return;

    const current =
      activeImages[certificationId] ?? 0;

    const next =
      (current + direction + totalImages) %
      totalImages;

    setActiveImages((previous) => ({
      ...previous,
      [certificationId]: next,
    }));
  };

  // ============================================================
  // SELECT IMAGE
  // ============================================================

  const selectImage = (
    certificationId: number,
    index: number
  ) => {
    setActiveImages((previous) => ({
      ...previous,
      [certificationId]: index,
    }));
  };

  // ============================================================
  // OPEN LIGHTBOX
  // ============================================================

  const openLightbox = (
    certificationId: number,
    imageIndex: number
  ) => {
    setLightbox({
      certificationId,
      imageIndex,
    });
  };

  // ============================================================
  // LOADING
  // ============================================================

  if (loading) {
    return (
      <>
        <style>{`
          .certification-loading-section {
            position: relative;
            overflow: hidden;
          }

          .certification-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);
}

          .certification-loading-icon {
            animation:
              certificationIconPulse
              2s ease-in-out infinite;
          }

          @keyframes certificationIconPulse {
            0%,
            100% {
              transform: scale(1);
            }

            50% {
              transform: scale(1.06);
            }
          }

          .certification-loading-line {
            position: relative;
            width: 180px;
            height: 4px;
            margin: 0 auto;
            overflow: hidden;
            border-radius: 999px;
            background: rgba(25, 135, 84, 0.08);
          }

          .certification-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:
              certificationLoadingMove
              2.2s linear infinite;
          }

          @keyframes certificationLoadingMove {
            0% {
              left: -40%;
            }

            100% {
              left: 100%;
            }
          }
        `}</style>

        <section
          id="certification"
          className="py-5 certification-loading-section"
          style={{
            background:
              "linear-gradient(180deg, #ffffff 0%, #f8faf9 100%)",
          }}
        >
          <div className="container py-5">
            <div
              className="certification-loading-card text-center"
              style={{
                minHeight: "400px",
                display: "flex",
                flexDirection: "column",
                justifyContent: "center",
                alignItems: "center",
              }}
            >
              <div
                className="certification-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-award"
                  style={{
                    fontSize: "2rem",
                  }}
                ></i>
              </div>

              <h5
                className="fw-bold mt-4 mb-2"
                style={{
                  color: "#17221c",
                }}
              >
                Loading Certifications
              </h5>

              <p className="text-secondary mb-4">
                Preparing certification content...
              </p>

              <div className="certification-loading-line">
                <span></span>
              </div>
            </div>
          </div>
        </section>
      </>
    );
  }

  return (
    <>
      <style>{`
        .certification-section {
          position: relative;
          overflow: hidden;
        }

        /* ============================================================
           HEADER
        ============================================================ */

        .certification-header {
          position: relative;
        }

        .certification-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;
        }

        .certification-badge-dot {
          width: 7px;
          height: 7px;
          border-radius: 50%;
          background: #198754;
          box-shadow:
            0 0 0 5px rgba(25, 135, 84, 0.08);
          animation:
            certificationBadgePulse
            2s ease-in-out infinite;
        }

        @keyframes certificationBadgePulse {
          0%,
          100% {
            transform: scale(1);
            opacity: 1;
          }

          50% {
            transform: scale(1.3);
            opacity: 0.65;
          }
        }

        .certification-title-wrapper {
          display: inline-flex;
          flex-direction: column;
          align-items: center;
          width: 100%;
        }

        .certification-title {
          color: #17221c;
          font-size: clamp(2.2rem, 5vw, 3.7rem);
          line-height: 1.08;
          letter-spacing: -0.04em;
        }

        .certification-title-green {
          color: #198754;
        }

        /* ============================================================
           SMALL ANIMATED TITLE LINE
        ============================================================ */

        .certification-title-line {
          position: relative;
          width: 90px;
          height: 4px;
          margin-top: 16px;
          overflow: hidden;
          border-radius: 999px;
          background: rgba(25, 135, 84, 0.12);
        }

        .certification-title-line::before {
          content: "";
          position: absolute;
          inset: 0;
          background: linear-gradient(
            90deg,
            rgba(25, 135, 84, 0.06),
            rgba(25, 135, 84, 0.16),
            rgba(25, 135, 84, 0.06)
          );
        }

        .certification-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.35);
          animation:
            certificationTitleLineMove
            2.5s linear infinite;
        }

        @keyframes certificationTitleLineMove {
          0% {
            left: -40%;
          }

          100% {
            left: 100%;
          }
        }

        .certification-description {
          max-width: 720px;
          color: #6c757d;
          font-size: 1rem;
          line-height: 1.8;
        }

        /* ============================================================
           CARD
        ============================================================ */

        .certification-card {
  position: relative;

  display: flex;
  flex-direction: column;

  overflow: hidden;

  height: 100%;

  background: #ffffff;

  /* Border tetap 1px, tetapi lebih terlihat */
  border:
    1px solid
    rgba(25, 135, 84, 0.24);

  border-radius: 24px;

  /* Shadow dibuat lebih halus agar garis card
     tetap menjadi pembatas utama */
  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;
}

.certification-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 IMAGE
        ============================================================ */

        .certification-card-line {
          position: relative;
          width: 100%;
          height: 5px;
          flex-shrink: 0;
          overflow: hidden;
          background: rgba(25, 135, 84, 0.08);
          z-index: 5;
        }

        .certification-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)
          );
        }

        .certification-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:
            certificationCardLineMove
            3.2s linear infinite;
        }

        @keyframes certificationCardLineMove {
          0% {
            left: -30%;
          }

          100% {
            left: 100%;
          }
        }

        /* ============================================================
           IMAGE
        ============================================================ */

        .certification-image-area {
          position: relative;
          height: 260px;
          flex-shrink: 0;
          overflow: hidden;
          background:
            linear-gradient(
              135deg,
              #edf5f0,
              #ffffff
            );
        }

        .certification-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;
        }

        .certification-card:hover
        .certification-main-image {
          transform: scale(1.045);
        }

        .certification-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.3) 100%
            );
        }

        .certification-image-counter {
          position: absolute;
          right: 15px;
          bottom: 15px;
          z-index: 3;
        }

        .certification-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 BUTTON
        ============================================================ */

        .certification-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;
        }

        .certification-nav-button:hover {
          background: #ffffff;
          color: #198754;
          box-shadow:
            0 10px 25px rgba(0, 0, 0, 0.18);
          transform:
            translateY(-50%)
            scale(1.08);
        }

        .certification-nav-prev {
          left: 12px;
        }

        .certification-nav-next {
          right: 12px;
        }

        /* ============================================================
           DOTS
        ============================================================ */

        .certification-dots-wrapper {
          display: flex;
          align-items: center;
          justify-content: center;
          min-height: 28px;
          padding-top: 10px;
        }

        .certification-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;
        }

        .certification-dot:hover {
          transform: scale(1.15);
        }

        .certification-dot.active {
          width: 22px;
          background: #198754;
        }

        /* ============================================================
           CONTENT
        ============================================================ */

        .certification-card-content {
          display: flex;
          flex-direction: column;
          flex: 1;
          padding: 25px;
        }

        .certification-card-title {
          margin: 0 0 10px;
          color: #17221c;
          font-size: 1.22rem;
          line-height: 1.4;
          letter-spacing: -0.015em;
          text-align: center;
        }

        .certification-issuer {
          display: flex;
          align-items: center;
          justify-content: center;
          gap: 7px;
          margin-bottom: 18px;
          color: #198754;
          font-size: 0.88rem;
          font-weight: 700;
          text-align: center;
        }

        .certification-small-line {
          width: 42px;
          height: 3px;
          margin: 0 auto 18px;
          border-radius: 999px;
          background:
            linear-gradient(
              90deg,
              #198754,
              #42b883
            );
        }

        /* ============================================================
           INFORMATION
        ============================================================ */

        .certification-info {
  margin-bottom: 20px;

  padding: 16px;

  border-radius: 18px;

  background: #f8faf9;

  /* Sedikit lebih jelas, tetap tipis */
  border:
    1px solid
    rgba(25, 135, 84, 0.13);
}

        .certification-info-item {
          display: flex;
          align-items: flex-start;
          gap: 12px;
        }

        .certification-info-item
        + .certification-info-item {
          margin-top: 15px;
        }

        .certification-info-icon {
          display: flex;
          align-items: center;
          justify-content: center;
          flex-shrink: 0;
          width: 36px;
          height: 36px;
          border-radius: 10px;
          background: rgba(25, 135, 84, 0.1);
          color: #198754;
        }

        .certification-info-label {
          display: block;
          margin-bottom: 2px;
          color: #6c757d;
          font-size: 0.69rem;
          font-weight: 700;
          letter-spacing: 0.02em;
        }

        .certification-info-value {
          display: block;
          color: #17221c;
          font-size: 0.84rem;
          line-height: 1.55;
          word-break: break-word;
        }

        /* ============================================================
           DESCRIPTION
        ============================================================ */

        .certification-description-heading {
          display: flex;
          align-items: center;
          justify-content: center;
          gap: 7px;
          margin-bottom: 10px;
          color: #198754;
          font-size: 0.8rem;
          font-weight: 700;
        }

        .certification-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
        ============================================================ */

        .certification-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;
}

.certification-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);
}

        .certification-empty-card::before {
          content: "";
          position: absolute;
          top: 0;
          left: -30%;
          width: 30%;
          height: 4px;
          background:
            linear-gradient(
              90deg,
              transparent,
              #198754,
              #42b883,
              transparent
            );
          animation:
            certificationCardLineMove
            3s linear infinite;
        }

        .certification-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;
        }

        /* ============================================================
           FOOTER
        ============================================================ */

        .certification-footer {
          color: #6c757d;
          font-size: 0.84rem;
        }

        /* ============================================================
           LIGHTBOX
        ============================================================ */

        .certification-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:
            certificationLightboxIn
            0.25s ease;
        }

        @keyframes certificationLightboxIn {
          from {
            opacity: 0;
          }

          to {
            opacity: 1;
          }
        }

        .certification-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:
            certificationImageIn
            0.3s ease;
        }

        @keyframes certificationImageIn {
          from {
            opacity: 0;
            transform: scale(0.97);
          }

          to {
            opacity: 1;
            transform: scale(1);
          }
        }

        .certification-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;
        }

        .certification-lightbox-close:hover {
          background: #ffffff;
          transform: scale(1.07);
        }

        .certification-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;
        }

        .certification-lightbox-nav:hover {
          background: #ffffff;
          transform:
            translateY(-50%)
            scale(1.07);
        }

        .certification-lightbox-prev {
          left: 22px;
        }

        .certification-lightbox-next {
          right: 22px;
        }

        .certification-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) {
          .certification-title-line {
            width: 80px;
            height: 3px;
            margin-top: 14px;
          }

          .certification-card {
            border-radius: 20px;
          }

          .certification-card-content {
            padding: 22px;
          }

          .certification-image-area {
            height: 245px;
          }

          .certification-lightbox {
            padding: 20px;
          }

          .certification-lightbox-image {
            max-width: 94%;
            max-height: 78vh;
          }

          .certification-lightbox-prev {
            left: 10px;
          }

          .certification-lightbox-next {
            right: 10px;
          }
        }

        @media (max-width: 575.98px) {
          .certification-title-line {
            width: 70px;
            height: 3px;
          }

          .certification-card {
            border-radius: 18px;
          }

          .certification-card-line {
            height: 4px;
          }

          .certification-image-area {
            height: 230px;
          }

          .certification-card-content {
            padding: 20px;
          }

          .certification-nav-button {
            width: 37px;
            height: 37px;
          }

          .certification-lightbox-close {
            top: 15px;
            right: 15px;
          }

          .certification-lightbox-counter {
            bottom: 15px;
          }
        }
      `}</style>

      <section
        id="certification"
        className="py-5 certification-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 certification-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="certification-badge">
                <span className="certification-badge-dot"></span>

                <i className="bi bi-award-fill"></i>

                Achievements
              </span>
            </div>

            <div className="certification-title-wrapper">
              <h2 className="fw-bold mb-0 text-center certification-title">
                Professional{" "}
                <span className="certification-title-green">
                  Certifications
                </span>
              </h2>

              <div className="certification-title-line">
                <span></span>
              </div>
            </div>

            <p className="mx-auto mb-0 mt-3 text-center certification-description">
              Professional certifications and achievements
              that represent my competencies, experience,
              and commitment to continuously developing my
              expertise in technology and engineering.
            </p>
          </div>

          {/* ============================================================
              EMPTY
          ============================================================ */}

          {certifications.length === 0 && (
            <div
              className="certification-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="certification-empty-icon">
                <i
                  className="bi bi-award"
                  style={{
                    fontSize: "2.3rem",
                  }}
                ></i>
              </div>

              <h5
                className="fw-bold mt-4 mb-2"
                style={{
                  color: "#17221c",
                }}
              >
                No Certifications Yet
              </h5>

              <p className="text-secondary mb-0">
                Certification data will be displayed here
                after being added.
              </p>
            </div>
          )}

          {/* ============================================================
              CERTIFICATIONS
          ============================================================ */}

          {certifications.length > 0 && (
            <div className="row g-4">
              {certifications.map(
                (
                  certification,
                  certificationIndex
                ) => {
                  const images =
                    getImages(certification);

                  const activeIndex =
                    activeImages[
                      certification.id
                    ] ?? 0;

                  const currentImage =
                    images[activeIndex];

                  return (
                    <div
                      key={certification.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 +
                            certificationIndex *
                              0.1
                          }s,
                          transform .7s ease ${
                            0.1 +
                            certificationIndex *
                              0.1
                          }s
                        `,
                      }}
                    >
                      <article className="certification-card">

                        <div className="certification-card-line">
                          <span></span>
                        </div>

                        <div className="certification-image-area">
                          {currentImage ? (
                            <>
                              <img
                                key={
                                  currentImage.image
                                }
                                src={
                                  currentImage.image
                                }
                                alt={`${certification.name} - Gambar ${
                                  activeIndex + 1
                                }`}
                                onClick={() =>
                                  openLightbox(
                                    certification.id,
                                    activeIndex
                                  )
                                }
                                className="certification-main-image"
                              />

                              <div className="certification-image-overlay"></div>

                              {images.length >
                                1 && (
                                <div className="certification-image-counter">
                                  <span className="certification-counter-badge">
                                    <i className="bi bi-images me-1"></i>

                                    {activeIndex +
                                      1}{" "}
                                    /{" "}
                                    {
                                      images.length
                                    }
                                  </span>
                                </div>
                              )}

                              {images.length >
                                1 && (
                                <button
                                  type="button"
                                  className="certification-nav-button certification-nav-prev"
                                  onClick={() =>
                                    changeImage(
                                      certification.id,
                                      -1,
                                      images.length
                                    )
                                  }
                                  aria-label="Gambar sebelumnya"
                                >
                                  <i className="bi bi-chevron-left"></i>
                                </button>
                              )}

                              {images.length >
                                1 && (
                                <button
                                  type="button"
                                  className="certification-nav-button certification-nav-next"
                                  onClick={() =>
                                    changeImage(
                                      certification.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-award"
                                  style={{
                                    fontSize:
                                      "2.3rem",
                                  }}
                                ></i>
                              </div>
                            </div>
                          )}
                        </div>

                        {images.length > 1 && (
                          <div className="certification-dots-wrapper">
                            <div className="d-flex justify-content-center align-items-center gap-2">
                              {images.map(
                                (
                                  image,
                                  index
                                ) => {
                                  const active =
                                    index ===
                                    activeIndex;

                                  return (
                                    <button
                                      key={
                                        image.id ||
                                        index
                                      }
                                      type="button"
                                      onClick={() =>
                                        selectImage(
                                          certification.id,
                                          index
                                        )
                                      }
                                      className={`certification-dot ${
                                        active
                                          ? "active"
                                          : ""
                                      }`}
                                      aria-label={`Tampilkan gambar ${
                                        index + 1
                                      }`}
                                    />
                                  );
                                }
                              )}
                            </div>
                          </div>
                        )}

                        <div className="certification-card-content">

                          <h4 className="fw-bold certification-card-title">
                            {certification.name}
                          </h4>

                          <div className="certification-issuer">
                            <i className="bi bi-building"></i>

                            <span>
                              {certification.issuer}
                            </span>
                          </div>

                          <div className="certification-small-line"></div>

                          <div className="certification-info">

                            <div className="certification-info-item">
                              <div className="certification-info-icon">
                                <i className="bi bi-calendar3"></i>
                              </div>

                              <div>
                                <small className="certification-info-label">
                                  ISSUE DATE
                                </small>

                                <span className="certification-info-value">
                                  {formatDate(
                                    certification.issueDate
                                  )}
                                </span>
                              </div>
                            </div>

                            {certification.expiryDate && (
                              <div className="certification-info-item">
                                <div className="certification-info-icon">
                                  <i className="bi bi-calendar-check"></i>
                                </div>

                                <div>
                                  <small className="certification-info-label">
                                    VALID UNTIL
                                  </small>

                                  <span className="certification-info-value">
                                    {formatDate(
                                      certification.expiryDate
                                    )}
                                  </span>
                                </div>
                              </div>
                            )}

                            {certification.credentialId && (
                              <div className="certification-info-item">
                                <div className="certification-info-icon">
                                  <i className="bi bi-patch-check"></i>
                                </div>

                                <div
                                  style={{
                                    minWidth: 0,
                                  }}
                                >
                                  <small className="certification-info-label">
                                    CREDENTIAL ID
                                  </small>

                                  <span className="certification-info-value">
                                    {
                                      certification.credentialId
                                    }
                                  </span>
                                </div>
                              </div>
                            )}
                          </div>

                          {certification.description && (
                            <div>
                              <div className="certification-description-heading">
                                <i className="bi bi-text-paragraph"></i>

                                <span>
                                  DESCRIPTION
                                </span>
                              </div>

                              <p className="certification-card-description">
                                {
                                  certification.description
                                }
                              </p>
                            </div>
                          )}
                        </div>
                      </article>
                    </div>
                  );
                }
              )}
            </div>
          )}

          {/* ============================================================
              FOOTER
          ============================================================ */}

          {certifications.length > 0 && (
            <div
              className="text-center mt-5 pt-4 certification-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-patch-check-fill text-success"></i>

                <span>
                  Continuous learning and professional
                  development
                </span>
              </div>
            </div>
          )}
        </div>
      </section>

      {/* ================================================================
          LIGHTBOX
      ================================================================ */}

      {lightbox && (
        <div
          className="certification-lightbox"
          onClick={() => setLightbox(null)}
        >
          {(() => {
            const certification =
              certifications.find(
                (item) =>
                  item.id ===
                  lightbox.certificationId
              );

            if (!certification) {
              return null;
            }

            const images =
              getImages(certification);

            if (images.length === 0) {
              return null;
            }

            const currentImage =
              images[lightbox.imageIndex];

            return (
              <>
                <button
                  type="button"
                  className="certification-lightbox-close"
                  onClick={() =>
                    setLightbox(null)
                  }
                  aria-label="Tutup"
                >
                  <i className="bi bi-x-lg"></i>
                </button>

                <img
                  key={currentImage.image}
                  src={currentImage.image}
                  alt={`${certification.name} - Gallery image`}
                  className="certification-lightbox-image"
                  onClick={(event) =>
                    event.stopPropagation()
                  }
                />

                {images.length > 1 && (
                  <button
                    type="button"
                    className="certification-lightbox-nav certification-lightbox-prev"
                    onClick={(event) => {
                      event.stopPropagation();

                      setLightbox({
                        certificationId:
                          lightbox.certificationId,
                        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="certification-lightbox-nav certification-lightbox-next"
                    onClick={(event) => {
                      event.stopPropagation();

                      setLightbox({
                        certificationId:
                          lightbox.certificationId,
                        imageIndex:
                          (lightbox.imageIndex +
                            1) %
                          images.length,
                      });
                    }}
                    aria-label="Gambar berikutnya"
                  >
                    <i className="bi bi-chevron-right"></i>
                  </button>
                )}

                {images.length > 1 && (
                  <span className="certification-lightbox-counter">
                    {lightbox.imageIndex + 1}{" "}
                    / {images.length}
                  </span>
                )}
              </>
            );
          })()}
        </div>
      )}
    </>
  );
}