'use server';

import { cookies } from 'next/headers';
import { NextRequest, NextResponse } from 'next/server';
import { productCategoriesData } from '../data';

export async function GET(request: NextRequest, context: any) {
  const cookieStore = await cookies();
  const lang = cookieStore.get('locale')?.value || 'en';

  const params = (await context).params;

  const { id } = await params;

  let category = productCategoriesData.find((cat) => cat.id === id);

  if (!category) {
    return NextResponse.json({ error: 'Not found' }, { status: 404 });
  }

  if (lang === 'fa') {
    category = {
      ...category,
      title: category.fa_title || category.title,
      priceTypeTitle: category.fa_priceTypeTitle || category.priceTypeTitle,
      bestForTitle: category.fa_bestForTitle || category.bestForTitle,
      description: category.fa_description || category.description,
    };
  }

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