import { NextResponse } from 'next/server';
import { requireAuth } from '@/lib/auth-utils';
import { postgresClient } from '@/lib/services/postgres-client';

export async function GET() {
  const { error } = await requireAuth();
  if (error) return error;

  try {
    const result = await postgresClient.query(
      `SELECT id, company_name FROM companies WHERE is_active = true AND is_deleted = false ORDER BY company_name ASC`
    );
    const companies = result.rows.map((row) => ({
      id: Number(row.id),
      company_name: row.company_name,
    }));
    return NextResponse.json(companies);
  } catch (error) {
    console.error('Error fetching companies list:', error);
    return NextResponse.json({ error: 'Failed to fetch companies list' }, { status: 500 });
  }
}
