import { NextResponse } from 'next/server';

const API_BASE = 'https://api.socialbuddy.co/api/users';

const normalizeUser = (u: any) => ({
    userid: u._id,
    name: u.name,
    email: u.email,
    mobile: u.mobileNumber,
    role: u.role,
    createdAt: u.createdAt,
});

// GET by id
export async function GET(
    _req: Request,
    { params }: { params: { id: string } }
) {
    const res = await fetch(`${API_BASE}/${params.id}`);
    const data = await res.json();
    return NextResponse.json(normalizeUser(data));
}

// UPDATE (admin)
export async function PUT(
    req: Request,
    { params }: { params: { id: string } }
) {
    const body = await req.json();

    const res = await fetch(`${API_BASE}/${params.id}`, {
        method: 'PUT',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(body),
    });

    const data = await res.json();
    return NextResponse.json(normalizeUser(data));
}

// DELETE (admin)
export async function DELETE(
    _req: Request,
    { params }: { params: { id: string } }
) {
    await fetch(`${API_BASE}/${params.id}`, { method: 'DELETE' });
    return NextResponse.json({ success: true });
}
