import { withAuth } from '../../../../../lib/with-auth';

const API_BASE = process.env.NEXT_PUBLIC_API_URL_INTERNAL || 'http://recipe-api:8080';

export const POST = withAuth(async (_req, session, context) => {
  const { id } = await context.params;
  const productId = Number(id);
  if (!productId) return Response.json({ error: 'Ogiltigt id' }, { status: 400 });

  const res = await fetch(`${API_BASE}/api/products/${productId}/restore`, {
    method: 'POST',
    headers: { Authorization: `Bearer ${session.accessToken}` },
  });
  const data = await res.json().catch(() => ({}));
  return Response.json(data, { status: res.status });
});

export const DELETE = withAuth(async (_req, session, context) => {
  const { id } = await context.params;
  const productId = Number(id);
  if (!productId) return Response.json({ error: 'Ogiltigt id' }, { status: 400 });

  const res = await fetch(`${API_BASE}/api/products/${productId}/permanent`, {
    method: 'DELETE',
    headers: { Authorization: `Bearer ${session.accessToken}` },
  });
  const data = await res.json().catch(() => ({}));
  return Response.json(data, { status: res.status });
});
