import { NextResponse } from 'next/server';
import { cookies } from 'next/headers';
import { generatePosts, IPostData } from './data';

export async function GET(request: Request) {
  const { searchParams } = new URL(request.url);
  const lang = (await cookies()).get('lang')?.value || 'fa';
  const page = parseInt(searchParams.get('page') || '1', 10);
  const perPage = 10;

  const posts = generatePosts();
  const start = (page - 1) * perPage;
  const end = start + perPage;
  const paginatedPosts = posts.slice(start, end);

  const data = paginatedPosts.map((post) => {
    return {
      id: post.id,
      imageSrc: post.imageSrc,
      title: post.fa_title || post.title,
    };
  });

  return NextResponse.json({
    data,
    pagination: {
      page,
      perPage,
      total: posts.length,
      totalPages: Math.ceil(posts.length / perPage),
    },
  });
}
