import type { Metadata } from "next";
import { Reveal } from "@/component/common/Reveal";
import { privacyPolicyData } from "@/data/privacy-policyData";

// ISR: keep HTML cache lifetime short (s-maxage=3600) instead of static 1-year default.
export const revalidate = 3600;

export const metadata: Metadata = {
  title: "Privacy Policy",
  robots: { index: false, follow: false },
  description: "Privacy Policy of DesignShare by Elegant IT Limited.",
};

export default function PrivacyPolicyPage() {
  const renderText = (text: string) => {
    if (text.includes("https://designshare.net")) {
      const parts = text.split("https://designshare.net");
      return (
        <>
          {parts[0]}
          <a
            href="https://designshare.net"
            target="_blank"
            rel="noopener noreferrer"
            style={{
              color: "#1E5EFF",
              textDecoration: "underline",
              fontWeight: 600,
            }}
          >
            https://designshare.net
          </a>
          {parts[1]}
        </>
      );
    }
    if (text.includes("admin@designshare.net")) {
      const parts = text.split("admin@designshare.net");
      return (
        <>
          {parts[0]}
          <a
            href="mailto:admin@designshare.net"
            style={{
              color: "#1E5EFF",
              textDecoration: "underline",
              fontWeight: 600,
            }}
          >
            admin@designshare.net
          </a>
          {parts[1]}
        </>
      );
    }
    if (text.includes("The General Data Protection Regulation (GDPR)")) {
      const parts = text.split("The General Data Protection Regulation (GDPR)");
      return (
        <>
          {parts[0]}
          <a
            href="https://gdpr-info.eu/"
            target="_blank"
            rel="noopener noreferrer"
            style={{
              color: "#1E5EFF",
              textDecoration: "underline",
              fontWeight: 600,
            }}
          >
            The General Data Protection Regulation (GDPR)
          </a>
          {parts[1]}
        </>
      );
    }
    return text;
  };

  return (
    <div className="ds-page">
      <section
        className="ds-sec"
        style={{
          background: "#F4EFE1",
          paddingTop: "160px",
          paddingBottom: "120px",
        }}
      >
        <div className="ds-container" style={{ maxWidth: "800px" }}>
          <Reveal>
            <h1 className="ds-h-2" style={{ marginBottom: "20px" }}>
              {privacyPolicyData.title}
            </h1>
            <p
              className="ds-lede"
              style={{ marginBottom: "60px", opacity: 0.7 }}
            >
              Last updated: {privacyPolicyData.lastUpdated}
            </p>
          </Reveal>

          <Reveal>
            <div
              style={{
                display: "flex",
                flexDirection: "column",
                gap: "40px",
                color: "#222222",
                fontFamily:
                  "var(--font-jakarta, 'Plus Jakarta Sans'), system-ui, sans-serif",
                fontSize: "16px",
                lineHeight: "1.75",
              }}
            >
              {privacyPolicyData.sections.map((section, idx) => (
                <div key={idx}>
                  <h3
                    style={{
                      fontSize: "20px",
                      fontWeight: 800,
                      color: "#0F0F10",
                      marginBottom: "16px",
                    }}
                  >
                    {section.title}
                  </h3>

                  {/* Render paragraphs up to list items */}
                  {section.paragraphs &&
                    section.paragraphs.map((p, pIdx) => {
                      // For CAN-SPAM Act, we place listItems after the second paragraph (pIdx === 1)
                      if (section.title === "CAN-SPAM Act:" && pIdx === 2) {
                        return (
                          <div key={pIdx}>
                            {section.listItems && (
                              <ul
                                style={{
                                  paddingLeft: "20px",
                                  display: "flex",
                                  flexDirection: "column",
                                  gap: "8px",
                                  listStyleType: "disc",
                                  marginBottom: "16px",
                                }}
                              >
                                {section.listItems.map((item, lIdx) => (
                                  <li key={lIdx}>
                                    {item.label && (
                                      <strong>{item.label}</strong>
                                    )}
                                    {item.text}
                                  </li>
                                ))}
                              </ul>
                            )}
                            <p style={{ marginBottom: "16px" }}>
                              {renderText(p)}
                            </p>
                          </div>
                        );
                      }
                      return (
                        <p key={pIdx} style={{ marginBottom: "16px" }}>
                          {renderText(p)}
                        </p>
                      );
                    })}

                  {/* Render standard list items for non-CAN-SPAM sections */}
                  {section.title !== "CAN-SPAM Act:" && section.listItems && (
                    <ul
                      style={{
                        paddingLeft: "20px",
                        display: "flex",
                        flexDirection: "column",
                        gap: "12px",
                        listStyleType: "disc",
                      }}
                    >
                      {section.listItems.map((item, lIdx) => (
                        <li key={lIdx}>
                          {item.label && <strong>{item.label} </strong>}
                          {item.text}
                        </li>
                      ))}
                    </ul>
                  )}

                  {/* Render extra list items for CAN-SPAM Act */}
                  {section.title === "CAN-SPAM Act:" &&
                    section.extraListItems && (
                      <ul
                        style={{
                          paddingLeft: "20px",
                          display: "flex",
                          flexDirection: "column",
                          gap: "8px",
                          listStyleType: "disc",
                          marginBottom: "16px",
                        }}
                      >
                        {section.extraListItems.map((item, elIdx) => (
                          <li key={elIdx}>{item.text}</li>
                        ))}
                      </ul>
                    )}
                </div>
              ))}
            </div>
          </Reveal>
        </div>
      </section>
    </div>
  );
}
