import type { Metadata } from "next";
import Link from "next/link";

type Guide = {
  slug: string;
  title: string;
  description: string;
  body: string[];
  faq: { q: string; a: string }[];
};

const GUIDES: readonly Guide[] = [
  {
    slug: "best-image-format-for-web",
    title: "Best image format for web (JPG vs PNG vs WebP vs AVIF)",
    description:
      "Learn which image format to use for websites: JPG/JPEG, PNG, WebP, and AVIF. Compare quality, compression, and compatibility.",
    body: [
      "When you upload images to a website, the format you choose affects page speed, SEO, and how sharp your image looks. The best approach is to convert and compress based on the type of image: photos, UI screenshots, icons, or transparent graphics.",
      "Use JPG/JPEG for photos. JPEG compression is great for gradients and camera images. If you need smaller files, convert JPG to WebP or AVIF and compare results at similar quality.",
      "Use PNG for UI screenshots, logos, and transparency. PNG is lossless, so it preserves crisp edges and text. If your PNG is too large, consider converting PNG to WebP (lossy or lossless) depending on your needs.",
      "Use WebP for modern web delivery. WebP often compresses images better than JPG at similar quality, and it can keep transparency like PNG. Converting PNG to WebP is one of the most common website optimizations.",
      "Use AVIF when you want the smallest files. AVIF can deliver excellent compression for photos. Convert JPG to AVIF for modern browsers; keep JPG or PNG fallback if you need maximum compatibility.",
    ],
    faq: [
      {
        q: "Which is better: WebP or AVIF?",
        a: "AVIF often compresses smaller for photos, while WebP is very fast and widely supported. For best results, convert an image to both and compare file size at similar quality.",
      },
      {
        q: "Is PNG always better quality than JPG?",
        a: "PNG is lossless but can be huge for photos. JPEG is designed for photos and can look great at much smaller sizes. Choose based on image type and your compression needs.",
      },
    ],
  },
  {
    slug: "how-to-compress-images-without-quality-loss",
    title: "How to compress images without obvious quality loss",
    description:
      "Practical steps to compress images (JPG, PNG, WebP, AVIF) while keeping them sharp. Learn when to resize and which format to convert to.",
    body: [
      "If you want to compress an image without obvious quality loss, start with the easiest win: resize to the actual display size. Many images are uploaded at 2–5× larger than needed.",
      "For photos, compress using JPG/JPEG quality around 80–90, or convert JPG to WebP/AVIF for smaller files at similar quality.",
      "For screenshots, logos, and UI, keep PNG when you need perfect edges and transparency. If the PNG is too large, try palette PNG or convert PNG to WebP (lossless or high-quality lossy).",
      "Always compare results at 100% zoom for text and edges. The goal is to reduce file size while keeping the image looking natural.",
    ],
    faq: [
      {
        q: "What’s the best quality setting for JPG compression?",
        a: "A common sweet spot is 80–90 for photos. If you see artifacts, increase quality; if file size is still large, convert to WebP or AVIF.",
      },
      {
        q: "Should I compress PNG or convert PNG to WebP?",
        a: "If you need PNG features (lossless pixels, transparency), compress with PNG presets. For web delivery, converting PNG to WebP often reduces file size more.",
      },
    ],
  },
  {
    slug: "webp-vs-png",
    title: "WebP vs PNG: which should you use?",
    description:
      "Compare WebP vs PNG for web images. Learn when to keep PNG, when to convert to WebP, and how compression impacts quality.",
    body: [
      "PNG is a lossless image format that’s excellent for transparency, sharp UI, and screenshots. The downside is file size: PNG can be much larger than necessary for web delivery.",
      "WebP is a modern image format designed for the web. It can compress images much smaller than PNG while keeping good quality, and it supports transparency too.",
      "If you need pixel-perfect results (like UI assets), keep PNG or use a palette PNG preset. If you’re optimizing a website, converting PNG to WebP is often the fastest way to improve page speed.",
      "For photos, WebP or AVIF usually wins. For graphics and text-heavy screenshots, test both formats to avoid ringing or blur.",
    ],
    faq: [
      {
        q: "Does WebP support transparency like PNG?",
        a: "Yes. WebP supports alpha transparency. Convert PNG to WebP when you want transparency with smaller file sizes.",
      },
      {
        q: "Why does my WebP look blurry?",
        a: "If you used lossy compression with low quality, edges and text can blur. Increase quality or use lossless WebP for UI/screenshot content.",
      },
    ],
  },
] as const;

export function generateStaticParams() {
  return GUIDES.map((g) => ({ slug: g.slug }));
}

export async function generateMetadata({
  params,
}: {
  params: Promise<{ slug: string }>;
}): Promise<Metadata> {
  const { slug } = await params;
  const g = GUIDES.find((x) => x.slug === slug);
  if (!g) return { title: "Guide" };
  return { title: g.title, description: g.description };
}

export default async function GuidePage({
  params,
}: {
  params: Promise<{ slug: string }>;
}) {
  const { slug } = await params;
  const g = GUIDES.find((x) => x.slug === slug);
  if (!g) {
    return (
      <main className="relative z-10 min-h-screen px-4 py-10 sm:px-6">
        <div className="mx-auto w-full max-w-3xl">
          <h1 className="font-display text-2xl font-bold tracking-tight text-slate-900">Guide not found</h1>
          <p className="mt-2 text-slate-600">Go back to guides.</p>
          <Link href="/guides" className="mt-5 inline-flex rounded-xl bg-white/70 px-4 py-2 text-sm font-semibold text-indigo-700 shadow-sm backdrop-blur hover:bg-white">
            Browse guides
          </Link>
        </div>
      </main>
    );
  }

  return (
    <main className="relative z-10 min-h-screen px-4 py-10 sm:px-6">
      <div className="mx-auto w-full max-w-4xl">
        <nav className="glass-panel flex flex-wrap items-center justify-between gap-3 rounded-2xl px-4 py-2.5 sm:px-6">
          <Link href="/" className="font-display rounded-xl px-2 py-2 text-sm font-bold tracking-tight text-slate-900 hover:bg-white/50">
            ← Image Converter
          </Link>
          <div className="flex flex-wrap items-center gap-1 text-sm font-medium">
            <Link className="rounded-xl px-3 py-2 text-slate-600 hover:bg-white/60" href="/guides">
              Guides
            </Link>
            <Link className="rounded-xl px-3 py-2 text-slate-600 hover:bg-white/60" href="/blog">
              Blog
            </Link>
          </div>
        </nav>

        <article className="glass-panel mt-10 rounded-3xl p-6 sm:p-8">
          <h1 className="font-display text-balance text-3xl font-extrabold tracking-tight text-slate-900 sm:text-4xl">
            {g.title}
          </h1>
          <p className="mt-3 max-w-3xl text-sm leading-relaxed text-slate-600 sm:text-base">{g.description}</p>

          <div className="prose prose-slate mt-8 max-w-none">
            {g.body.map((p) => (
              <p key={p}>{p}</p>
            ))}
            <h2>FAQ</h2>
            {g.faq.map((f) => (
              <div key={f.q}>
                <p>
                  <strong>{f.q}</strong>
                </p>
                <p>{f.a}</p>
              </div>
            ))}
          </div>
        </article>
      </div>
    </main>
  );
}

