import { ArrowUpRight } from "lucide-react";
import Image, { type StaticImageData } from "next/image";
import Link from "next/link";
import fallbackBlogImg from "@/app/blog/_assets/blog-hero.png";
import { Container } from "@/app/components/layout/Container";
import Heading from "@/app/components/ui/Heading";
import fetchGraphQL from "@/app/graphql/graphql";

interface BlogCardProps {
  image?: string | StaticImageData | null;
  alt?: string;
  title: string;
  href: string;
  imgWidth: string;
  textWidth: string;
  className?: string;
}

function BlogCard({
  image,
  alt,
  imgWidth,
  textWidth,
  title,
  href,
  className,
}: BlogCardProps) {
  return (
    <div
      className={`relative flex rounded-2xl overflow-hidden border border-gray-100 ${className}`}
    >
      <Link href={href} className={imgWidth}>
        <Image
          src={image || fallbackBlogImg}
          alt={alt || title}
          width={500}
          height={500}
          className="h-full w-full object-cover"
        />
      </Link>
      <div className={`px-8 py-8 pb-18 ${textWidth}`}>
        <Link href={href}>
          <Heading as="h3" variant="card" className="text-primary">
            {title}
          </Heading>
        </Link>
        <Link
          href={href}
          className="absolute right-5 bottom-6 rounded-full border border-gray-200 p-3"
        >
          <ArrowUpRight size={16} strokeWidth={0.5} className="text-black/80" />
        </Link>
      </div>
    </div>
  );
}

export async function Blogs() {
  const query = `query GetLatestPosts($first: Int!){
        posts(first: $first) {
            nodes {
                title
                slug
                featuredImage {
                    node {
                        altText
                        sourceUrl
                    }
                }
            }
        }
    }`;

  const res: {
    posts?: {
      nodes?: {
        title: string;
        slug: string;
        featuredImage?: {
          node?: { altText?: string | null; sourceUrl?: string | null } | null;
        } | null;
      }[];
    };
  } = await fetchGraphQL(query, { first: 3 });
  const blogsData = res?.posts?.nodes ?? [];

  if (blogsData.length === 0) {
    return null;
  }

  return (
    <section className="flex justify-center mt-32">
      <Container>
        <div className="flex flex-col gap-8 lg:gap-16">
          <div className="flex flex-col gap-2">
            <Heading
              as="h2"
              variant="section"
              className="text-primary text-center"
            >
              Take a Look at Our Articles & Resources
            </Heading>
            <div className="mx-auto w-full lg:w-4/5">
              <p className="text-center text-gray-500">
                Discover valuable insights and information about Basmati rice,
                its quality, and our commitment to excellence in rice
                exportation.
              </p>
            </div>
          </div>
          <div className="flex flex-col md:flex-row items-center gap-6">
            <BlogCard
              title={blogsData[0].title}
              href={`/${blogsData[0].slug}`}
              image={blogsData[0].featuredImage?.node?.sourceUrl}
              alt={blogsData[0].featuredImage?.node?.altText || blogsData[0].title}
              imgWidth="w-full"
              textWidth="w-full"
              className="flex-col h-fit w-full lg:w-1/2"
            />
            {blogsData.length > 1 && (
              <div className="flex flex-col gap-6 lg:gap-12">
                {blogsData.slice(1).map((blog) => (
                  <BlogCard
                    key={blog.slug}
                    image={blog.featuredImage?.node?.sourceUrl}
                    alt={blog.featuredImage?.node?.altText || blog.title}
                    title={blog.title}
                    href={`/${blog.slug}`}
                    imgWidth="w-full lg:w-1/2"
                    textWidth="w-full lg:w-2/3"
                    className="h-full flex-col md:flex-row"
                  />
                ))}
              </div>
            )}
          </div>
        </div>
      </Container>
    </section>
  );
}
