import { ImageResponse } from 'next/og';
import { getPostBySlug } from '@/lib/blog';
import { OGImageTemplate } from '@/components/OGImageTemplate';

export const runtime = 'nodejs';

export default async function Image({ params: { locale, slug } }: { params: { locale: string, slug: string } }) {
  const post = await getPostBySlug(slug, locale);

  if (!post) {
    return new ImageResponse(
      <div style={{ display: 'flex', width: '100%', height: '100%', backgroundColor: '#001a4d' }} />
    );
  }

  const featuredImage = post.frontmatter.featuredImage
    ? (post.frontmatter.featuredImage.startsWith('http')
        ? post.frontmatter.featuredImage
        : `https://klz-cables.com${post.frontmatter.featuredImage}`)
    : undefined;

  return new ImageResponse(
    (
      <OGImageTemplate
        title={post.frontmatter.title}
        description={post.frontmatter.excerpt}
        label={post.frontmatter.category || 'Blog'}
        image={featuredImage}
      />
    ),
    {
      width: 1200,
      height: 630,
    }
  );
}
