import { NextResponse } from 'next/server';
import { cookies } from 'next/headers';
import { posts } from '../data';

export async function GET(request: Request, context: any) {
  const params = await context.params;
  const postId = params.id;

  const cookieStore = await cookies();
  const lang = cookieStore.get('locale')?.value || 'en';

  let post = posts.find((post) => post.id === postId);
  if (!post) {
    return NextResponse.json({ error: 'Not found' }, { status: 404 });
  }

  if (lang === 'fa') {
    post = {
      ...post,
      title: post.fa_title || post.title,
      content: post.fa_content || post.content,
    };
  }

  return NextResponse.json({
    data: post,
  });
}
