import { NextRequest, NextResponse } from 'next/server';

// 정적 리소스 호스트 (배너/팝업/회원 사진/게시판 이미지 등).
// 로컬: NEXT_PUBLIC_RESOURCE_URL 미설정 시 API_URL 로 폴백 (API 가 wwwroot 정적 서빙).
// dev/prod: dev-resource.antooza.com / resource.antooza.com 가 Admin/API uploads 통합 서빙.
const RESOURCE_URL = process.env.NEXT_PUBLIC_RESOURCE_URL || process.env.API_URL;

export async function GET(_: NextRequest, { params }: { params: Promise<{ path: string[] }> })
{
	const { path } = await params;
	const filePath = `/uploads/${path.join('/')}`;

	try {
		const res = await fetch(`${RESOURCE_URL}${filePath}`, {
			cache: 'no-store'
		});

		if (!res.ok) {
			return new NextResponse(null, {
				status: res.status
			});
		}

		const contentType = res.headers.get('content-type') || 'application/octet-stream';
		const buffer = await res.arrayBuffer();

		return new NextResponse(buffer, {
			status: 200,
			headers: {
				'Content-Type': contentType,
				'Cache-Control': 'public, max-age=31536000, immutable'
			}
		});
	} catch {
		return new NextResponse(null, {
			status: 502
		});
	}
}