"use client";

import { useEffect, useState } from "react";

type Service = {
  id: number;
  title: string;
  slug: string;
  description: string | null;
  icon: string | null;
  image: string | null;
  featured: boolean;
  active: boolean;
};

export default function Services() {
  const [services, setServices] = useState<Service[]>([]);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    const getServices = async () => {
      try {
        const response = await fetch("/api/services");

        if (!response.ok) {
          throw new Error("Failed to fetch services");
        }

        const result = await response.json();

        if (result.success && Array.isArray(result.data)) {
          setServices(
            result.data.filter(
              (service: Service) => service.active
            )
          );
        } else {
          setServices([]);
        }
      } catch (error) {
        console.error("GET SERVICES:", error);
        setServices([]);
      } finally {
        setLoading(false);
      }
    };

    getServices();
  }, []);

  if (loading) {
    return (
      <>
        <section id="services" className="services-section">
          <div className="services-container">
            <div className="services-loading">
              <div className="services-loading-line">
                <span />
              </div>

              <div className="services-loading-content">
                <div
                  className="spinner-border text-success"
                  role="status"
                  aria-hidden="true"
                />

                <p>Loading services...</p>
              </div>
            </div>
          </div>
        </section>

        <style>{`
          .services-section {
            width: 100%;
            padding: 90px 20px;
            background: #ffffff;
          }

          .services-container {
            width: 100%;
            max-width: 1200px;
            margin: 0 auto;
          }

          .services-loading {
            position: relative;
            width: 100%;
            max-width: 420px;
            min-height: 170px;
            margin: 0 auto;
            overflow: hidden;
            background: #ffffff;
            border: 1px solid rgba(25, 135, 84, 0.35);
            border-radius: 16px;
            box-shadow: 0 10px 30px rgba(25, 50, 38, 0.06);
          }

          .services-loading-line {
            position: relative;
            width: 100%;
            height: 3px;
            overflow: hidden;
            background: rgba(25, 135, 84, 0.12);
          }

          .services-loading-line span {
            position: absolute;
            top: 0;
            left: -40%;
            width: 40%;
            height: 100%;
            border-radius: 999px;
            background: linear-gradient(
              90deg,
              transparent,
              #198754,
              #73c69a,
              #198754,
              transparent
            );
            box-shadow: 0 0 10px rgba(25, 135, 84, 0.4);
            animation: loadingLine 2.4s linear infinite;
          }

          @keyframes loadingLine {
            0% {
              left: -40%;
            }

            100% {
              left: 100%;
            }
          }

          .services-loading-content {
            min-height: 167px;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
          }

          .services-loading-content p {
            margin: 14px 0 0;
            color: #68746d;
            font-size: 0.88rem;
          }

          @media (max-width: 575.98px) {
            .services-section {
              padding: 60px 15px;
            }
          }
        `}</style>
      </>
    );
  }

  if (services.length === 0) {
    return null;
  }

  return (
    <>
      <section id="services" className="services-section">
        <div className="services-container">

          {/* =====================================================
              HEADER
          ====================================================== */}

          <div className="services-header">

            <div className="services-label">
              <span className="services-label-line"></span>

              <span className="services-label-text">
                <i
                  className="bi bi-layers"
                  aria-hidden="true"
                />
                SERVICES
              </span>

              <span className="services-label-line"></span>
            </div>

            <h2 className="services-title">
              Professional{" "}
              <span>Services</span>
            </h2>

            <div className="services-title-line">
              <span />
            </div>

            <p className="services-intro">
              Professional engineering and technology
              services tailored to business and technical
              needs through modern digital solutions,
              automation, and software development.
            </p>

          </div>

          {/* =====================================================
              SERVICES GRID
          ====================================================== */}

          <div className="services-grid">

            {services.map((service, index) => (
              <article
                key={service.id}
                className={`service-card ${
                  service.featured
                    ? "service-card-featured"
                    : ""
                }`}
              >

                {/* =================================================
                    ANIMATED TOP LINE
                ================================================== */}

                <div className="service-top-line">
                  <span />
                </div>

                {/* =================================================
                    FEATURED
                ================================================== */}

                {service.featured && (
                  <div className="service-featured">
                    <i
                      className="bi bi-star-fill"
                      aria-hidden="true"
                    />

                    <span>Featured</span>
                  </div>
                )}

                {/* =================================================
                    IMAGE
                ================================================== */}

                {service.image && (
                  <div className="service-image-wrapper">

                    <img
                      src={service.image}
                      alt={service.title}
                      className="service-image"
                    />

                    <div className="service-image-overlay" />

                    <div className="service-image-status">
                      <i
                        className="bi bi-image"
                        aria-hidden="true"
                      />

                      <span>Service</span>
                    </div>

                  </div>
                )}

                {/* =================================================
                    CONTENT
                ================================================== */}

                <div className="service-content">

                  {/* ICON */}

                  <div className="service-icon-wrapper">
                    <div className="service-icon">

                      <i
                        className={
                          service.icon ||
                          "bi bi-code-square"
                        }
                        aria-hidden="true"
                      />

                    </div>
                  </div>

                  {/* TITLE */}

                  <h3 className="service-title">
                    {service.title}
                  </h3>

                  {/* SLUG */}

                  {service.slug && (
                    <span className="service-slug">
                      {service.slug}
                    </span>
                  )}

                  {/* DESCRIPTION */}

                  {service.description && (
  <div className="service-description-wrapper">
    <p className="service-description">
      {service.description}
    </p>
  </div>
)}

                  {/* FOOTER */}

                  <div className="service-footer">

                    <div className="service-footer-left">
                      <span className="service-number">
                        {String(index + 1).padStart(2, "0")}
                      </span>

                      <span className="service-footer-label">
                        Professional Solution
                      </span>
                    </div>

                    <span className="service-arrow">
                      <i
                        className="bi bi-arrow-up-right"
                        aria-hidden="true"
                      />
                    </span>

                  </div>

                </div>

              </article>
            ))}

          </div>

        </div>
      </section>

      {/* ==========================================================
          COMPONENT CSS
      =========================================================== */}

      <style>{`
        /* =====================================================
           SECTION
        ====================================================== */

        .services-section {
          position: relative;
          width: 100%;
          padding: 95px 20px;
          background: #ffffff;
          overflow: hidden;
        }

        .services-container {
          width: 100%;
          max-width: 1200px;
          margin: 0 auto;
        }

        /* =====================================================
           HEADER
        ====================================================== */

        .services-header {
          width: 100%;
          max-width: 820px;
          margin: 0 auto 55px;
          text-align: center;
        }

        .services-label {
          display: flex;
          align-items: center;
          justify-content: center;
          gap: 12px;
          margin-bottom: 14px;
          color: #198754;
          font-size: 0.72rem;
          font-weight: 700;
          letter-spacing: 0.17em;
        }

        .services-label-text {
          display: inline-flex;
          align-items: center;
          gap: 7px;
        }

        .services-label-text i {
          font-size: 0.78rem;
        }

        .services-label-line {
          width: 30px;
          height: 1px;
          background: #198754;
          opacity: 0.55;
        }

        .services-title {
          margin: 0;
          color: #17221c;
          font-size: clamp(
            2rem,
            4vw,
            3rem
          );
          font-weight: 700;
          line-height: 1.15;
          letter-spacing: -0.035em;
        }

        .services-title span {
          color: #198754;
        }

        /* =====================================================
           HEADER ANIMATED LINE
        ====================================================== */

        .services-title-line {
          position: relative;
          width: 125px;
          height: 3px;
          margin: 19px auto 20px;
          overflow: hidden;
          border-radius: 999px;
          background: rgba(25, 135, 84, 0.13);
        }

        .services-title-line span {
          position: absolute;
          top: 0;
          left: -55%;
          width: 55%;
          height: 100%;
          border-radius: 999px;

          background: linear-gradient(
            90deg,
            transparent,
            #198754,
            #73c69a,
            #4aa977,
            #198754,
            transparent
          );

          box-shadow:
            0 0 8px rgba(25, 135, 84, 0.38);

          animation:
            servicesHeadingLine
            2.8s linear infinite;
        }

        @keyframes servicesHeadingLine {
          0% {
            left: -55%;
          }

          100% {
            left: 100%;
          }
        }

        .services-intro {
          max-width: 730px;
          margin: 0 auto;
          color: #68746d;
          font-size: 0.96rem;
          line-height: 1.85;
          text-align: justify;
          text-align-last: center;
        }

        /* =====================================================
           GRID
        ====================================================== */

        .services-grid {
          display: grid;
          grid-template-columns:
            repeat(
              3,
              minmax(0, 1fr)
            );
          gap: 26px;
          align-items: stretch;
        }

        /* =====================================================
           CARD
        ====================================================== */

        .service-card {
          position: relative;
          display: flex;
          flex-direction: column;
          min-width: 0;
          height: 100%;
          overflow: hidden;

          background: #ffffff;

          /*
           * GREEN BORDER YANG JELAS
           */
          border: 1.5px solid
            rgba(
              25,
              135,
              84,
              0.52
            );

          border-radius: 18px;

          box-shadow:
            0 10px 30px
              rgba(
                25,
                50,
                38,
                0.065
              ),
            0 0 0 1px
              rgba(
                25,
                135,
                84,
                0.035
              );

          transition:
            transform 0.3s ease,
            box-shadow 0.3s ease,
            border-color 0.3s ease;
        }

        .service-card:hover {
          transform: translateY(-6px);

          border-color: #198754;

          box-shadow:
            0 18px 40px
              rgba(
                25,
                50,
                38,
                0.10
              ),
            0 0 0 1px
              rgba(
                25,
                135,
                84,
                0.08
              );
        }

        .service-card-featured {
          border-color:
            rgba(
              25,
              135,
              84,
              0.68
            );
        }

        /* =====================================================
           ANIMATED TOP CARD LINE
        ====================================================== */

        .service-top-line {
          position: relative;
          width: 100%;
          height: 3px;
          flex-shrink: 0;
          overflow: hidden;
          background:
            rgba(
              25,
              135,
              84,
              0.15
            );
        }

        .service-top-line span {
          position: absolute;
          top: 0;
          left: -35%;
          width: 35%;
          height: 100%;

          border-radius: 999px;

          background: linear-gradient(
            90deg,
            transparent,
            #198754,
            #73c69a,
            #4aa977,
            #198754,
            transparent
          );

          box-shadow:
            0 0 10px
              rgba(
                25,
                135,
                84,
                0.48
              );

          animation:
            serviceTopLine
            3s linear infinite;
        }

        @keyframes serviceTopLine {
          0% {
            left: -35%;
          }

          100% {
            left: 100%;
          }
        }

        /* =====================================================
           FEATURED
        ====================================================== */

        .service-featured {
          position: absolute;
          top: 17px;
          right: 17px;
          z-index: 5;

          display: inline-flex;
          align-items: center;
          gap: 6px;

          padding: 7px 11px;

          color: #198754;

          background:
            rgba(
              255,
              255,
              255,
              0.95
            );

          border: 1px solid
            rgba(
              25,
              135,
              84,
              0.30
            );

          border-radius: 999px;

          font-size: 0.68rem;
          font-weight: 700;

          box-shadow:
            0 7px 18px
              rgba(
                0,
                0,
                0,
                0.07
              );
        }

        .service-featured i {
          font-size: 0.65rem;
        }

        /* =====================================================
           IMAGE
        ====================================================== */

        .service-image-wrapper {
          position: relative;
          width: 100%;
          height: 215px;
          overflow: hidden;
          background: #f3f7f5;

          border-bottom:
            1px solid
            rgba(
              25,
              135,
              84,
              0.18
            );
        }

        .service-image {
          display: block;
          width: 100%;
          height: 100%;

          /*
           * Tidak ada zoom.
           */
          object-fit: cover;

          transition:
            opacity 0.35s ease;
        }

        .service-card:hover .service-image {
          opacity: 0.97;
        }

        .service-image-overlay {
          position: absolute;
          inset: 0;
          pointer-events: none;

          background:
            linear-gradient(
              to bottom,
              transparent 50%,
              rgba(
                0,
                0,
                0,
                0.10
              )
            );
        }

        .service-image-status {
          position: absolute;
          left: 15px;
          bottom: 14px;

          display: inline-flex;
          align-items: center;
          gap: 6px;

          padding: 6px 9px;

          color: #ffffff;

          background:
            rgba(
              25,
              135,
              84,
              0.90
            );

          border-radius: 7px;

          font-size: 0.65rem;
          font-weight: 600;

          backdrop-filter: blur(5px);
        }

        .service-image-status i {
          font-size: 0.68rem;
        }

        /* =====================================================
           CONTENT
        ====================================================== */

        .service-content {
          position: relative;

          display: flex;
          flex-direction: column;

          flex: 1;

          padding: 28px 26px 23px;
        }

        /* =====================================================
           ICON
        ====================================================== */

        .service-icon-wrapper {
          margin-bottom: 18px;
        }

        .service-icon {
          width: 52px;
          height: 52px;

          display: flex;
          align-items: center;
          justify-content: center;

          color: #198754;

          background:
            rgba(
              25,
              135,
              84,
              0.08
            );

          border: 1px solid
            rgba(
              25,
              135,
              84,
              0.25
            );

          border-radius: 13px;

          font-size: 1.35rem;

          transition:
            background 0.3s ease,
            color 0.3s ease,
            transform 0.3s ease;
        }

        .service-card:hover .service-icon {
          background: #198754;
          color: #ffffff;
          transform: translateY(-3px);
        }

        /* =====================================================
           TITLE
        ====================================================== */

        .service-title {
          width: 100%;
          margin: 0 0 7px;

          color: #17221c;

          font-size: 1.23rem;
          font-weight: 700;
          line-height: 1.4;

          text-align: center;

          letter-spacing: -0.02em;
        }

        /* =====================================================
           SLUG
        ====================================================== */

        .service-slug {
          display: block;

          margin-bottom: 18px;

          color: #198754;

          font-size: 0.68rem;
          font-weight: 600;

          letter-spacing: 0.05em;

          text-align: center;

          opacity: 0.82;

          overflow-wrap: anywhere;
        }

        /* =====================================================
           DESCRIPTION WRAPPER
        ====================================================== */

        .service-description-wrapper {
  min-width: 0;
  margin-top: 2px;
}

       
        /* =====================================================
           DESCRIPTION
        ====================================================== */

        .service-description {
          flex: 1;
          min-width: 0;

          margin: 0;

          color: #68746d;

          font-size: 0.9rem;
          line-height: 1.82;

          text-align: justify;
          text-justify: inter-word;

          overflow-wrap: anywhere;
        }

        /* =====================================================
           FOOTER
        ====================================================== */

        .service-footer {
          display: flex;
          align-items: center;
          justify-content: space-between;

          gap: 15px;

          margin-top: auto;
          padding-top: 21px;

          border-top:
            1px solid
            rgba(
              25,
              135,
              84,
              0.15
            );
        }

        .service-footer-left {
          display: flex;
          align-items: center;
          gap: 9px;

          min-width: 0;
        }

        .service-number {
          flex-shrink: 0;

          color: #198754;

          font-size: 0.7rem;
          font-weight: 700;

          letter-spacing: 0.1em;
        }

        .service-footer-label {
          color: #8a948e;

          font-size: 0.7rem;
          font-weight: 500;

          white-space: nowrap;
        }

        .service-arrow {
          flex-shrink: 0;

          width: 36px;
          height: 36px;

          display: flex;
          align-items: center;
          justify-content: center;

          color: #198754;

          background:
            rgba(
              25,
              135,
              84,
              0.08
            );

          border: 1px solid
            rgba(
              25,
              135,
              84,
              0.17
            );

          border-radius: 10px;

          font-size: 0.82rem;

          transition:
            background 0.3s ease,
            color 0.3s ease,
            transform 0.3s ease;
        }

        .service-card:hover .service-arrow {
          background: #198754;
          color: #ffffff;

          transform:
            translate(
              2px,
              -2px
            );
        }

        /* =====================================================
           TABLET
        ====================================================== */

        @media (max-width: 991.98px) {

          .services-section {
            padding: 75px 18px;
          }

          .services-header {
            margin-bottom: 45px;
          }

          .services-grid {
            grid-template-columns:
              repeat(
                2,
                minmax(0, 1fr)
              );

            gap: 21px;
          }

          .service-image-wrapper {
            height: 200px;
          }

          .service-content {
            padding: 25px 22px 21px;
          }

          .service-title {
            font-size: 1.17rem;
          }

          .service-description {
            font-size: 0.88rem;
            line-height: 1.78;
          }

          .service-footer-label {
            font-size: 0.67rem;
          }
        }

        /* =====================================================
           MOBILE
        ====================================================== */

        @media (max-width: 767.98px) {

          .services-section {
            padding: 65px 15px;
          }

          .services-header {
            margin-bottom: 40px;
          }

          .services-label {
            font-size: 0.67rem;
          }

          .services-label-line {
            width: 21px;
          }

          .services-title {
            font-size: 1.95rem;
          }

          .services-title-line {
            width: 105px;
            margin-top: 16px;
            margin-bottom: 18px;
          }

          .services-intro {
            font-size: 0.88rem;
            line-height: 1.78;

            text-align: justify;
            text-align-last: center;
          }

          .services-grid {
            grid-template-columns: 1fr;
            gap: 20px;
          }

          .service-card {
            border-radius: 16px;
          }

          .service-image-wrapper {
            height: 205px;
          }

          .service-content {
            padding: 24px 20px 20px;
          }

          .service-icon {
            width: 48px;
            height: 48px;
            border-radius: 12px;
            font-size: 1.25rem;
          }

          .service-title {
            font-size: 1.16rem;
            line-height: 1.4;
            text-align: center;
          }

          .service-slug {
            margin-bottom: 16px;
          }


          .service-description {
            font-size: 0.87rem;
            line-height: 1.76;
            text-align: justify;
          }

          .service-footer {
            padding-top: 19px;
          }

          .service-featured {
            top: 13px;
            right: 13px;
            padding: 6px 9px;
            font-size: 0.64rem;
          }
        }

        /* =====================================================
           SMALL MOBILE
        ====================================================== */

        @media (max-width: 420px) {

          .services-section {
            padding: 55px 12px;
          }

          .services-title {
            font-size: 1.72rem;
          }

          .services-intro {
            font-size: 0.84rem;
          }

          .services-grid {
            gap: 17px;
          }

          .service-image-wrapper {
            height: 180px;
          }

          .service-content {
            padding: 22px 17px 18px;
          }

          .service-icon {
            width: 45px;
            height: 45px;
            font-size: 1.17rem;
          }

          .service-title {
            font-size: 1.08rem;
          }

          .service-description {
            font-size: 0.84rem;
            line-height: 1.72;
          }

          .service-footer-label {
            font-size: 0.62rem;
          }

          .service-arrow {
            width: 33px;
            height: 33px;
            font-size: 0.75rem;
          }
        }

        /* =====================================================
           REDUCED MOTION
        ====================================================== */

        @media (prefers-reduced-motion: reduce) {

          .services-title-line span,
          .service-top-line span,
          .service-description-line span {
            animation: none !important;
          }

          .service-card,
          .service-image,
          .service-icon,
          .service-arrow {
            transition: none !important;
          }
        }
      `}</style>
    </>
  );
}

