import Head from "next/head";
import { ReactNode } from "react";

export interface SeoHeadConfig {
  title?: string;
  description?: string;
  keywords?: string;
  url?: string;
  siteName?: string;
  img?: string;
  canonical?: string;
  pre?: string;
  next?: string;
  jsonLd?: string;
}

interface SeoHeadProps {
  seoConfig: SeoHeadConfig;
  children?: ReactNode;
}

export function SeoHead({ seoConfig, children }: SeoHeadProps) {
  return (
    <Head>
      {seoConfig.title ? <title>{seoConfig.title}</title> : null}

      {seoConfig.description ? (
        <meta name="description" content={seoConfig.description} />
      ) : null}

      {seoConfig.keywords ? (
        <meta name="keywords" content={seoConfig.keywords} />
      ) : null}

      {seoConfig.url ? (
        <meta property="og:url" key="og:url" content={seoConfig.url} />
      ) : null}

      {seoConfig.siteName ? (
        <meta
          property="og:site_name"
          key="og:site_name"
          content={seoConfig.siteName}
        />
      ) : null}

      {seoConfig.title ? (
        <meta property="og:title" key="og:title" content={seoConfig.title} />
      ) : null}

      {seoConfig.description ? (
        <meta
          property="og:description"
          key="og:description"
          content={seoConfig.description}
        />
      ) : null}

      {seoConfig.img ? (
        <meta property="og:image" key="og:image" content={seoConfig.img} />
      ) : null}

      {seoConfig.title ? (
        <meta
          name="twitter:title"
          key="twitter:title"
          content={seoConfig.title}
        />
      ) : null}

      {seoConfig.description ? (
        <meta
          name="twitter:description"
          key="twitter:description"
          content={seoConfig.description}
        />
      ) : null}

      {seoConfig.img ? (
        <meta
          name="twitter:image"
          key="twitter:image"
          content={seoConfig.img}
        />
      ) : null}

      {seoConfig.canonical ? (
        <link rel="canonical" key="canonical" href={seoConfig.canonical} />
      ) : null}

      {seoConfig.pre ? (
        <link
          rel="prev"
          href={`https://${siteConfig.host}/novel${seoConfig.pre}`}
        />
      ) : null}

      {seoConfig.next ? (
        <link
          rel="next"
          href={`https://${siteConfig.host}/novel${seoConfig.next}`}
        />
      ) : null}

      {seoConfig.jsonLd ? (
        <script
          type="application/ld+json"
          key="application/ld+json"
          dangerouslySetInnerHTML={{ __html: seoConfig.jsonLd }}
        />
      ) : null}
      {children}
    </Head>
  );
}
