"use client";

import { useEffect, useRef, useState } from "react";
import Link from "next/link";

type ArticleImage = {
  id: number;
  image: string;
  articleId: number;
  createdAt: string;
};

type Article = {
  id: number;
  title: string;
  slug: string;
  excerpt: string | null;
  content: string;
  thumbnail: string | null;
  category: string | null;
  published: boolean;
  publishedAt: string | null;
  articleimage: ArticleImage[];
};

export default function Blog() {
  const [articles, setArticles] = useState<Article[]>([]);
  const [loading, setLoading] = useState(true);

  const [activeImages, setActiveImages] = useState<
    Record<number, number>
  >({});

  const [lightbox, setLightbox] = useState<{
    articleId: number;
    imageIndex: number;
  } | null>(null);

  const [visibleArticles, setVisibleArticles] = useState<
    Record<number, boolean>
  >({});

  const articleRefs = useRef<
    Record<number, HTMLDivElement | null>
  >({});

  /* =====================================================
     GET ARTICLES
  ===================================================== */
  useEffect(() => {
    const getArticles = async () => {
      try {
        const response = await fetch("/api/articles");
        const result = await response.json();

        if (result.success) {
          const publishedArticles = result.data.filter(
            (article: Article) => article.published === true
          );

          setArticles(publishedArticles);
        }
      } catch (error) {
        console.error("GET ARTICLES:", error);
      } finally {
        setLoading(false);
      }
    };

    getArticles();
  }, []);

  /* =====================================================
     SCROLL REVEAL
  ===================================================== */
  useEffect(() => {
    if (loading || articles.length === 0) return;

    const observer = new IntersectionObserver(
      (entries) => {
        entries.forEach((entry) => {
          if (!entry.isIntersecting) return;

          const id = Number(
            entry.target.getAttribute("data-article-id")
          );

          if (!id) return;

          setVisibleArticles((previous) => ({
            ...previous,
            [id]: true,
          }));

          observer.unobserve(entry.target);
        });
      },
      {
        threshold: 0.12,
        rootMargin: "0px 0px -40px 0px",
      }
    );

    articles.forEach((article) => {
      const element = articleRefs.current[article.id];

      if (element) {
        observer.observe(element);
      }
    });

    return () => {
      observer.disconnect();
    };
  }, [articles, loading]);

  /* =====================================================
     FORMAT DATE
  ===================================================== */
  const formatDate = (date: string | null) => {
    if (!date) return "";

    return new Date(date).toLocaleDateString("id-ID", {
      day: "2-digit",
      month: "long",
      year: "numeric",
    });
  };

  /* =====================================================
     CHANGE ARTICLE IMAGE
  ===================================================== */
  const changeImage = (
    articleId: number,
    direction: "next" | "prev",
    totalImages: number
  ) => {
    setActiveImages((previous) => {
      const current = previous[articleId] || 0;

      let nextIndex = current;

      if (direction === "next") {
        nextIndex =
          current === totalImages - 1
            ? 0
            : current + 1;
      }

      if (direction === "prev") {
        nextIndex =
          current === 0
            ? totalImages - 1
            : current - 1;
      }

      return {
        ...previous,
        [articleId]: nextIndex,
      };
    });
  };

  /* =====================================================
     SELECT IMAGE
  ===================================================== */
  const selectImage = (
    articleId: number,
    imageIndex: number
  ) => {
    setActiveImages((previous) => ({
      ...previous,
      [articleId]: imageIndex,
    }));
  };

  /* =====================================================
     OPEN LIGHTBOX
  ===================================================== */
  const openLightbox = (
    articleId: number,
    imageIndex: number
  ) => {
    setLightbox({
      articleId,
      imageIndex,
    });
  };

  /* =====================================================
     LIGHTBOX NAVIGATION
  ===================================================== */
  const changeLightboxImage = (
    direction: "next" | "prev"
  ) => {
    if (!lightbox) return;

    const article = articles.find(
      (item) => item.id === lightbox.articleId
    );

    if (
      !article ||
      article.articleimage.length === 0
    ) {
      return;
    }

    const totalImages =
      article.articleimage.length;

    setLightbox((previous) => {
      if (!previous) return previous;

      let nextIndex = previous.imageIndex;

      if (direction === "next") {
        nextIndex =
          previous.imageIndex === totalImages - 1
            ? 0
            : previous.imageIndex + 1;
      }

      if (direction === "prev") {
        nextIndex =
          previous.imageIndex === 0
            ? totalImages - 1
            : previous.imageIndex - 1;
      }

      return {
        ...previous,
        imageIndex: nextIndex,
      };
    });
  };

  return (
    <>
      {/* =================================================
          CSS BIASA
          Tidak menggunakan style jsx
      ================================================= */}
      <style>{`
        .blog-section {
          position: relative;
          overflow: hidden;
        }

        /* ================================================
           TITLE
        ================================================ */

        .article-title-wrapper {
          position: relative;
          display: inline-flex;
          flex-direction: column;
          align-items: center;
        }

        .latest-title-line {
          position: relative;
          width: 82%;
          max-width: 270px;
          height: 4px;
          margin-top: 15px;
          overflow: hidden;
          border-radius: 999px;
          background: rgba(25, 135, 84, 0.13);
        }

        .latest-title-line::before {
          content: "";
          position: absolute;
          inset: 0;
          background: linear-gradient(
            90deg,
            transparent,
            rgba(25, 135, 84, 0.35),
            transparent
          );
        }

        .latest-title-line span {
          position: absolute;
          top: 0;
          left: -35%;
          width: 35%;
          height: 100%;
          border-radius: 999px;
          background: linear-gradient(
            90deg,
            transparent,
            #198754,
            #42b883,
            #198754,
            transparent
          );
          animation: titleLineMove 2.8s ease-in-out infinite;
        }

        @keyframes titleLineMove {
          0% {
            left: -35%;
          }

          50% {
            left: 100%;
          }

          100% {
            left: 100%;
          }
        }

        /* ================================================
           ARTICLE CARD
        ================================================ */

        .article-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;
}

.article-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);
}

        /* ================================================
           ANIMATED GREEN TOP LINE
        ================================================ */

        .article-card-line {
  position: relative;
  width: 100%;
  height: 5px;
  flex-shrink: 0;
  overflow: hidden;
  background: rgba(25, 135, 84, 0.08);
  z-index: 5;
}

.article-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)
  );
}

.article-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 8px rgba(25, 135, 84, 0.35);

  animation:
    cardLineMove 3s linear infinite;
}

@keyframes cardLineMove {
  0% {
    left: -30%;
  }

  100% {
    left: 100%;
  }
}

        /* ================================================
           ARTICLE IMAGE
        ================================================ */

        .article-main-image {
          transition:
            transform 0.7s
            cubic-bezier(0.22, 1, 0.36, 1);
        }

        .article-card:hover
        .article-main-image {
          transform: scale(1.035);
        }

        /* ================================================
           IMAGE NAVIGATION
        ================================================ */

        .image-nav-button {
          width: 38px;
          height: 38px;
          padding: 0;

          background: rgba(
            255,
            255,
            255,
            0.92
          );

          border: none;

          backdrop-filter: blur(10px);

          z-index: 3;

          transition:
            transform 0.25s ease,
            background 0.25s ease,
            box-shadow 0.25s ease;
        }

        .image-nav-button:hover {
          background: #ffffff;

          box-shadow:
            0 8px 20px
            rgba(0, 0, 0, 0.15);

          transform: scale(1.05);
        }

        /* ================================================
           EMPTY STATE
        ================================================ */

        .article-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;
}

.article-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);
}

        .article-empty-card::before {
          content: "";

          position: absolute;

          top: 0;
          left: -30%;

          width: 30%;
          height: 3px;

          background: linear-gradient(
            90deg,
            transparent,
            #198754,
            #42b883,
            transparent
          );

          animation:
            cardLineMove 3s linear infinite;
        }

        /* ================================================
           READ ARTICLE BUTTON
        ================================================ */

        .read-article-btn {
          position: relative;

          display: flex;
          align-items: center;
          justify-content: space-between;

          width: 100%;
          min-height: 48px;

          padding:
            7px
            8px
            7px
            17px;

          overflow: hidden;

          border:
            1px solid
            rgba(25, 135, 84, 0.12);

          border-radius: 14px;

          background: #f7fbf9;

          color: #198754;

          font-size: 0.88rem;
          font-weight: 700;

          transition:
            transform 0.3s ease,
            background 0.3s ease,
            border-color 0.3s ease,
            box-shadow 0.3s ease;
        }

        .read-article-btn:hover {
          color: #ffffff;

          background: #198754;

          border-color: #198754;

          transform:
            translateY(-2px);

          box-shadow:
            0 10px 25px
            rgba(25, 135, 84, 0.2);
        }

        .read-article-text {
          position: relative;
          z-index: 2;
        }

        .read-article-icon {
          position: relative;

          z-index: 2;

          display: flex;
          align-items: center;
          justify-content: center;

          width: 34px;
          height: 34px;

          border-radius: 10px;

          background:
            rgba(25, 135, 84, 0.1);

          transition:
            transform 0.3s ease,
            background 0.3s ease;
        }

        .read-article-btn:hover
        .read-article-icon {
          background:
            rgba(255, 255, 255, 0.16);

          transform:
            translate(2px, -2px);
        }

        .read-article-shine {
          position: absolute;

          top: 0;
          left: -100%;

          width: 60%;
          height: 100%;

          background: linear-gradient(
            90deg,
            transparent,
            rgba(255, 255, 255, 0.18),
            transparent
          );

          transform:
            skewX(-20deg);

          animation:
            buttonShine 4s
            ease-in-out infinite;
        }

        @keyframes buttonShine {
          0%,
          65% {
            left: -100%;
          }

          85%,
          100% {
            left: 140%;
          }
        }

        /* ================================================
           PULSE DOT
        ================================================ */

        .blog-pulse-dot {
          animation:
            blogPulse 2s
            ease-in-out infinite;
        }

        @keyframes blogPulse {
          0%,
          100% {
            transform: scale(1);
            opacity: 1;
          }

          50% {
            transform: scale(1.35);
            opacity: 0.65;
          }
        }

        /* ================================================
           RESPONSIVE
        ================================================ */

        @media (max-width: 767.98px) {
          .latest-title-line {
            width: 75%;
            max-width: 220px;
            margin-top: 13px;
          }

          .article-card {
            border-radius: 20px;
          }
        }

        @media (max-width: 575.98px) {
          .latest-title-line {
            width: 70%;
            max-width: 190px;
            height: 3px;
          }

          .article-card {
            border-radius: 18px;
          }

          .article-card-line {
            height: 3px;
          }
        }
      `}</style>

      {/* =================================================
          BLOG SECTION
      ================================================= */}
      <section
        id="blog"
        className="py-5 blog-section"
        style={{
          background:
            "radial-gradient(circle at 10% 10%, rgba(25,135,84,0.07), transparent 32%), radial-gradient(circle at 90% 30%, rgba(13,110,253,0.05), transparent 30%), #ffffff",
        }}
      >
        <div className="container py-5">

          {/* =================================================
              HEADER
          ================================================= */}
          <div className="text-center mb-5 pb-lg-3">

            <div className="mb-3">
              <span
                className="d-inline-flex align-items-center gap-2 px-3 py-2 rounded-pill fw-bold"
                style={{
                  background:
                    "rgba(25, 135, 84, 0.09)",
                  color: "#198754",
                  border:
                    "1px solid rgba(25, 135, 84, 0.12)",
                  fontSize: "0.78rem",
                  letterSpacing: "0.08em",
                  textTransform: "uppercase",
                }}
              >
                <span
                  className="blog-pulse-dot"
                  style={{
                    width: "7px",
                    height: "7px",
                    borderRadius: "50%",
                    background: "#198754",
                    boxShadow:
                      "0 0 0 5px rgba(25,135,84,0.08)",
                  }}
                />

                Blog & Insights
              </span>
            </div>

            {/* TITLE */}
            <div className="article-title-wrapper">
              <h2
                className="fw-bold mb-0 text-center"
                style={{
                  fontSize:
                    "clamp(2.2rem, 5vw, 3.7rem)",
                  lineHeight: "1.08",
                  letterSpacing: "-0.04em",
                  color: "#17221c",
                }}
              >
                Latest Articles
              </h2>

              {/* ANIMATED LINE */}
              <div className="latest-title-line">
                <span></span>
              </div>
            </div>

            {/* DESCRIPTION */}
            <p
              className="text-secondary mx-auto mb-0 text-center mt-3"
              style={{
                maxWidth: "760px",
                fontSize: "1.02rem",
                lineHeight: "1.8",
                textAlign: "center",
              }}
            >
              Explore articles, experiences, tutorials,
              and insights on technology, software
              development, engineering, and digital
              solutions.
            </p>
          </div>

          {/* =================================================
              LOADING
          ================================================= */}
          {loading && (
            <div className="row g-4">

              {[1, 2, 3].map((item) => (
                <div
                  key={item}
                  className="col-md-6 col-lg-4"
                >
                  <div className="article-card overflow-hidden">

                    <div className="article-card-line">
                      <span></span>
                    </div>

                    <div
                      style={{
                        height: "245px",
                        background: "#edf0ee",
                      }}
                    />

                    <div className="p-4">

                      <div
                        className="mb-3"
                        style={{
                          height: "14px",
                          width: "30%",
                          borderRadius: "999px",
                          background: "#edf0ee",
                        }}
                      />

                      <div
                        className="mb-3"
                        style={{
                          height: "22px",
                          width: "85%",
                          borderRadius: "999px",
                          background: "#edf0ee",
                        }}
                      />

                      <div
                        className="mb-2"
                        style={{
                          height: "13px",
                          width: "90%",
                          borderRadius: "999px",
                          background: "#edf0ee",
                        }}
                      />

                      <div
                        className="mb-4"
                        style={{
                          height: "13px",
                          width: "70%",
                          borderRadius: "999px",
                          background: "#edf0ee",
                        }}
                      />

                      <div
                        style={{
                          height: "42px",
                          width: "100%",
                          borderRadius: "12px",
                          background: "#edf0ee",
                        }}
                      />

                    </div>
                  </div>
                </div>
              ))}

            </div>
          )}

          {/* =================================================
              EMPTY STATE
          ================================================= */}
          {!loading &&
            articles.length === 0 && (
              <div
                className="text-center mx-auto article-empty-card"
                style={{
                  maxWidth: "500px",
                  padding: "55px 30px",
                }}
              >

                <div
                  className="d-flex align-items-center justify-content-center mx-auto"
                  style={{
                    width: "74px",
                    height: "74px",
                    borderRadius: "20px",
                    background:
                      "rgba(25,135,84,0.08)",
                    color: "#198754",
                    fontSize: "2rem",
                  }}
                >
                  <i className="bi bi-journal-text"></i>
                </div>

                <h4 className="fw-bold mt-4 mb-2">
                  No Articles Yet
                </h4>

                <p className="text-secondary mb-0">
                  There are currently no articles
                  published. Please check back later
                  to discover the latest content.
                </p>

              </div>
            )}

          {/* =================================================
              ARTICLES
          ================================================= */}
          {!loading &&
            articles.length > 0 && (
              <div className="row g-4">

                {articles.map((article, index) => {

                  const currentImage =
                    activeImages[article.id] || 0;

                  const hasImages =
                    article.articleimage &&
                    article.articleimage.length > 0;

                  const isVisible =
                    visibleArticles[article.id];

                  return (
                    <div
                      key={article.id}
                      className="col-md-6 col-lg-4"
                      ref={(element) => {
                        articleRefs.current[
                          article.id
                        ] = element;
                      }}
                      data-article-id={
                        article.id
                      }
                    >

                      <article
                        className="article-card h-100"
                        style={{
                          opacity:
                            isVisible ? 1 : 0,

                          transform:
                            isVisible
                              ? "translateY(0)"
                              : "translateY(35px)",

                          transition:
                            "opacity 0.7s ease, transform 0.7s cubic-bezier(0.22,1,0.36,1)",

                          transitionDelay:
                            isVisible
                              ? `${Math.min(
                                  index * 80,
                                  400
                                )}ms`
                              : "0ms",
                        }}
                      >

                        {/* ANIMATED TOP LINE */}
                        <div className="article-card-line">
  <span></span>
</div>

                        {/* =================================================
                            IMAGE
                        ================================================= */}
                        {hasImages ? (
                          <>

                            <div
                              className="position-relative overflow-hidden"
                              style={{
                                height: "245px",
                                background:
                                  "#eef5f1",
                              }}
                            >

                              <img
                                src={
                                  article
                                    .articleimage[
                                    currentImage
                                  ].image
                                }
                                alt={
                                  article.title
                                }
                                className="w-100 h-100 article-main-image"
                                onClick={() =>
                                  openLightbox(
                                    article.id,
                                    currentImage
                                  )
                                }
                                style={{
                                  objectFit:
                                    "cover",
                                  cursor:
                                    "zoom-in",
                                }}
                              />

                              <div
                                className="position-absolute top-0 start-0 w-100 h-100"
                                style={{
                                  background:
                                    "linear-gradient(to bottom, rgba(0,0,0,0.02) 20%, rgba(0,0,0,0.38) 100%)",
                                  pointerEvents:
                                    "none",
                                }}
                              />

                              {article
                                .articleimage
                                .length > 1 && (
                                <>

                                  {/* PREVIOUS */}
                                  <button
                                    type="button"
                                    aria-label="Gambar sebelumnya"
                                    className="btn rounded-circle position-absolute top-50 start-0 translate-middle-y ms-3 d-flex align-items-center justify-content-center shadow-sm image-nav-button"
                                    onClick={() =>
                                      changeImage(
                                        article.id,
                                        "prev",
                                        article
                                          .articleimage
                                          .length
                                      )
                                    }
                                  >
                                    <i className="bi bi-chevron-left"></i>
                                  </button>

                                  {/* NEXT */}
                                  <button
                                    type="button"
                                    aria-label="Gambar berikutnya"
                                    className="btn rounded-circle position-absolute top-50 end-0 translate-middle-y me-3 d-flex align-items-center justify-content-center shadow-sm image-nav-button"
                                    onClick={() =>
                                      changeImage(
                                        article.id,
                                        "next",
                                        article
                                          .articleimage
                                          .length
                                      )
                                    }
                                  >
                                    <i className="bi bi-chevron-right"></i>
                                  </button>

                                  {/* COUNTER */}
                                  <span
                                    className="position-absolute bottom-0 end-0 mb-3 me-3 text-white fw-bold"
                                    style={{
                                      padding:
                                        "7px 11px",
                                      borderRadius:
                                        "999px",
                                      background:
                                        "rgba(0,0,0,0.55)",
                                      backdropFilter:
                                        "blur(10px)",
                                      fontSize:
                                        "0.72rem",
                                      zIndex: 3,
                                    }}
                                  >
                                    {currentImage +
                                      1}{" "}
                                    /{" "}
                                    {
                                      article
                                        .articleimage
                                        .length
                                    }
                                  </span>

                                </>
                              )}

                            </div>

                            {/* DOTS */}
                            {article.articleimage
                              .length > 1 && (
                              <div
                                className="article-dots"
                                style={{
                                  display:
                                    "flex",
                                  justifyContent:
                                    "center",
                                  alignItems:
                                    "center",
                                  gap: "6px",
                                  minHeight:
                                    "25px",
                                  paddingTop:
                                    "10px",
                                }}
                              >
                                {article
                                  .articleimage
                                  .map(
                                    (
                                      img,
                                      imageIndex
                                    ) => (
                                      <button
                                        key={
                                          img.id
                                        }
                                        type="button"
                                        onClick={() =>
                                          selectImage(
                                            article.id,
                                            imageIndex
                                          )
                                        }
                                        aria-label={`Gambar ${
                                          imageIndex +
                                          1
                                        }`}
                                        style={{
                                          width:
                                            imageIndex ===
                                            currentImage
                                              ? "20px"
                                              : "7px",
                                          height:
                                            "7px",
                                          padding: 0,
                                          border:
                                            "0",
                                          borderRadius:
                                            "10px",
                                          background:
                                            imageIndex ===
                                            currentImage
                                              ? "#198754"
                                              : "#d2d9d5",
                                          cursor:
                                            "pointer",
                                          transition:
                                            "width 0.25s ease, background 0.25s ease",
                                        }}
                                      />
                                    )
                                  )}
                              </div>
                            )}

                          </>
                        ) : article.thumbnail ? (

                          <div
                            className="position-relative overflow-hidden"
                            style={{
                              height: "245px",
                              background:
                                "#eef5f1",
                            }}
                          >

                            <img
                              src={
                                article.thumbnail
                              }
                              alt={
                                article.title
                              }
                              className="w-100 h-100 article-main-image"
                              style={{
                                objectFit:
                                  "cover",
                              }}
                            />

                            <div
                              className="position-absolute top-0 start-0 w-100 h-100"
                              style={{
                                background:
                                  "linear-gradient(to bottom, rgba(0,0,0,0.02) 20%, rgba(0,0,0,0.35) 100%)",
                              }}
                            />

                          </div>

                        ) : (

                          <div
                            className="d-flex align-items-center justify-content-center"
                            style={{
                              height: "245px",
                              background:
                                "linear-gradient(135deg, #edf8f2, #f7fbf9)",
                            }}
                          >

                            <div
                              className="d-flex align-items-center justify-content-center"
                              style={{
                                width: "76px",
                                height: "76px",
                                borderRadius:
                                  "20px",
                                background:
                                  "rgba(25,135,84,0.1)",
                              }}
                            >
                              <i className="bi bi-journal-richtext fs-2 text-success"></i>
                            </div>

                          </div>

                        )}

                        {/* =================================================
                            CONTENT
                        ================================================= */}
                        <div
                          className="p-4 d-flex flex-column"
                          style={{
                            minHeight: "285px",
                          }}
                        >

                          <div
                            className="d-flex align-items-center justify-content-between gap-2 mb-3"
                            style={{
                              minHeight: "30px",
                            }}
                          >

                            {article.category ? (
                              <span
                                className="fw-bold"
                                style={{
                                  display:
                                    "inline-flex",
                                  alignItems:
                                    "center",
                                  padding:
                                    "7px 12px",
                                  borderRadius:
                                    "999px",
                                  background:
                                    "rgba(25,135,84,0.08)",
                                  color:
                                    "#198754",
                                  fontSize:
                                    "0.72rem",
                                }}
                              >
                                {
                                  article.category
                                }
                              </span>
                            ) : (
                              <span></span>
                            )}

                            {article.publishedAt && (
                              <span
                                className="text-secondary"
                                style={{
                                  fontSize:
                                    "0.76rem",
                                  whiteSpace:
                                    "nowrap",
                                }}
                              >
                                <i className="bi bi-calendar3 me-1"></i>

                                {formatDate(
                                  article.publishedAt
                                )}
                              </span>
                            )}

                          </div>

                          {/* TITLE */}
                          <h3
                            className="fw-bold mb-3"
                            style={{
                              color:
                                "#17221c",
                              fontSize:
                                "1.28rem",
                              lineHeight:
                                "1.4",
                              letterSpacing:
                                "-0.015em",
                              display:
                                "-webkit-box",
                              WebkitBoxOrient:
                                "vertical",
                              WebkitLineClamp:
                                2,
                              overflow:
                                "hidden",
                            }}
                          >
                            {article.title}
                          </h3>

                          {/* EXCERPT */}
                          {article.excerpt && (
                            <p
                              className="text-secondary mb-4"
                              style={{
                                fontSize:
                                  "0.93rem",
                                lineHeight:
                                  "1.75",
                                display:
                                  "-webkit-box",
                                WebkitBoxOrient:
                                  "vertical",
                                WebkitLineClamp:
                                  3,
                                overflow:
                                  "hidden",
                                textAlign:
                                  "justify",
                              }}
                            >
                              {
                                article.excerpt
                              }
                            </p>
                          )}

                          {/* BUTTON */}
                          <div className="mt-auto pt-2">

                            <Link
                              href={`/blog/${article.slug}`}
                              className="read-article-btn text-decoration-none"
                            >

                              <span className="read-article-text">
                                Read Article
                              </span>

                              <span className="read-article-icon">
                                <i className="bi bi-arrow-up-right"></i>
                              </span>

                              <span className="read-article-shine"></span>

                            </Link>

                          </div>

                        </div>

                      </article>

                    </div>
                  );
                })}

              </div>
            )}

          {/* =================================================
              LIGHTBOX
          ================================================= */}
          {lightbox && (
            <div
              className="position-fixed top-0 start-0 w-100 h-100 d-flex align-items-center justify-content-center"
              style={{
                background:
                  "rgba(0,0,0,0.88)",
                zIndex: 9999,
                padding: "30px",
              }}
              onClick={() =>
                setLightbox(null)
              }
            >

              {/* CLOSE */}
              <button
                type="button"
                className="btn rounded-circle position-absolute top-0 end-0 m-4 d-flex align-items-center justify-content-center"
                style={{
                  width: "45px",
                  height: "45px",
                  background:
                    "rgba(255,255,255,0.9)",
                  border: "none",
                  zIndex: 10,
                }}
                onClick={() =>
                  setLightbox(null)
                }
                aria-label="Tutup"
              >
                <i className="bi bi-x-lg"></i>
              </button>

              {/* IMAGE */}
              <img
                src={
                  articles.find(
                    (article) =>
                      article.id ===
                      lightbox.articleId
                  )?.articleimage[
                    lightbox.imageIndex
                  ]?.image
                }
                alt="Article image"
                style={{
                  maxWidth: "90%",
                  maxHeight: "85vh",
                  objectFit: "contain",
                  borderRadius: "12px",
                }}
                onClick={(event) =>
                  event.stopPropagation()
                }
              />

              {/* PREVIOUS */}
              <button
                type="button"
                className="btn rounded-circle position-absolute top-50 start-0 translate-middle-y ms-4 d-flex align-items-center justify-content-center"
                style={{
                  width: "48px",
                  height: "48px",
                  background:
                    "rgba(255,255,255,0.9)",
                  border: "none",
                  zIndex: 10,
                }}
                onClick={(event) => {
                  event.stopPropagation();

                  changeLightboxImage(
                    "prev"
                  );
                }}
                aria-label="Gambar sebelumnya"
              >
                <i className="bi bi-chevron-left"></i>
              </button>

              {/* NEXT */}
              <button
                type="button"
                className="btn rounded-circle position-absolute top-50 end-0 translate-middle-y me-4 d-flex align-items-center justify-content-center"
                style={{
                  width: "48px",
                  height: "48px",
                  background:
                    "rgba(255,255,255,0.9)",
                  border: "none",
                  zIndex: 10,
                }}
                onClick={(event) => {
                  event.stopPropagation();

                  changeLightboxImage(
                    "next"
                  );
                }}
                aria-label="Gambar berikutnya"
              >
                <i className="bi bi-chevron-right"></i>
              </button>

              {/* COUNTER */}
              <span
                className="position-absolute bottom-0 start-50 translate-middle-x mb-4 text-white fw-bold"
                style={{
                  padding: "7px 13px",
                  borderRadius: "999px",
                  background:
                    "rgba(0,0,0,0.6)",
                  fontSize: "0.8rem",
                }}
              >
                {lightbox.imageIndex + 1}{" "}
                /{" "}
                {
                  articles.find(
                    (article) =>
                      article.id ===
                      lightbox.articleId
                  )?.articleimage.length
                }
              </span>

            </div>
          )}

        </div>
      </section>
    </>
  );
}