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

export async function GET() {
  try {
    const result = await postgresClient.query(`
      SELECT
        r.instance_uid,
        r.name,
        r.backup_server_uid,
        bs.name as backup_server_name,
        r.capacity_bytes,
        r.free_space_bytes,
        r.used_space_bytes,
        r.repository_type,
        r.synced_at
      FROM veeam_repositories r
      LEFT JOIN veeam_backup_servers bs ON bs.instance_uid = r.backup_server_uid
      ORDER BY r.name
    `);

    return NextResponse.json(result.rows);
  } catch (error) {
    console.error('[VEEAM-API] repositories error:', error);
    return NextResponse.json([]);
  }
}
