import { NextResponse } from 'next/server';
import { getDb } from '@/lib/db';

export const dynamic = 'force-dynamic';

export async function GET() {
  try {
    const db = getDb();
    const row = db
      .prepare('SELECT value FROM settings WHERE key = ?')
      .get('phone_gateway_config');

    if (!row) {
      return NextResponse.json({ config: null });
    }

    const config = JSON.parse(row.value as string);
    // Remove password for security
    const { password: _, ...safeConfig } = config;

    return NextResponse.json({ config: safeConfig });
  } catch (error: any) {
    console.error('Failed to get phone gateway config:', error);
    return NextResponse.json(
      { config: null, error: 'Failed to load configuration' },
      { status: 500 }
    );
  }
}
