import { NextResponse } from 'next/server';
import { readFile } from 'node:fs/promises';
import { existsSync } from 'node:fs';
import { resolve } from '../../../../lib/server-container.js';
export async function GET(request) {
    try {
        const { searchParams } = new URL(request.url);
        const attachmentId = searchParams.get('id');
        if (!attachmentId) {
            return NextResponse.json({ error: 'Missing attachment id' }, { status: 400 });
        }
        const repo = resolve('IPmAttachmentRepository');
        const attachment = await repo.findById(attachmentId);
        if (!attachment) {
            return NextResponse.json({ error: 'Attachment not found' }, { status: 404 });
        }
        if (!existsSync(attachment.storagePath)) {
            return NextResponse.json({ error: 'File not found on disk' }, { status: 404 });
        }
        const buffer = await readFile(attachment.storagePath);
        return new NextResponse(buffer, {
            headers: {
                'Content-Type': attachment.mimeType,
                'Content-Disposition': `attachment; filename="${attachment.filename}"`,
                'Content-Length': String(buffer.length),
            },
        });
    }
    catch (error) {
        const message = error instanceof Error ? error.message : 'Download failed';
        return NextResponse.json({ error: message }, { status: 500 });
    }
}
