import { NextRequest, NextResponse } from 'next/server';
import { ResultDto } from '@/types/response/common';
import { fetchJson } from '@/lib/utils/server';

// 복수형 members 프록시 — /api/members/{sid}/track-record, /api/members/equipped-items 등
// (기존 단수형 /api/member 프록시와 별개 경로)
export async function GET(request: NextRequest, { params }: { params: Promise<{ path: string[] }> }) {
	const { path } = await params;
	const endpoint = `/api/members/${path.join('/')}`;
	const url = new URL(request.url);
	const res: ResultDto = await fetchJson(`${endpoint}${url.search}`, { method: 'GET' });
	return NextResponse.json(res);
}

// D3 지위 아이템 장착/해제 — POST /api/members/me/items/{id}/equip · /unequip (body 없음)
export async function POST(request: NextRequest, { params }: { params: Promise<{ path: string[] }> }) {
	const { path } = await params;
	const endpoint = `/api/members/${path.join('/')}`;
	const contentType = request.headers.get('content-type') || '';
	const raw = await request.arrayBuffer();

	if (raw.byteLength > 0) {
		const res: ResultDto = await fetchJson(endpoint, {
			method: 'POST',
			body: raw,
			headers: contentType ? { 'Content-Type': contentType } : undefined
		});
		return NextResponse.json(res);
	}

	const res: ResultDto = await fetchJson(endpoint, { method: 'POST' });
	return NextResponse.json(res);
}
