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

import fs from 'fs';
import path from 'path';

import { verifySuperAdminServerSide } from '@/services/auth';

export async function GET(
  request: NextRequest,
  context: { params: Promise<{ slug: string[] }> }
) {
  const cookie = request.headers.get('cookie') || '';
  const isSuperAdmin = await verifySuperAdminServerSide(cookie);

  if (!isSuperAdmin) {
    return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
  }

  const { slug } = await context.params;
  const filePath = path.join(process.cwd(), 'src', 'app', 'form-helpers', ...slug);

  try {
    const fileContent = fs.readFileSync(filePath, 'utf8');
    return new NextResponse(fileContent, {
      headers: {
        'Content-Type': 'text/html; charset=utf-8',
      },
    });
  } catch (error) {
    return NextResponse.json({ error: 'File not found' }, { status: 404 });
  }
}
