import { pgClients } from '@opengis/fastify-table/utils.js';

export default async function getDashboard({ pg = pgClients.client }) {
  try {
    const statsQuery = `
      SELECT
        (SELECT COUNT(*)::int FROM gis.services) as services_count,
        (SELECT COUNT(*)::int FROM gis.registers) as registers_count,
        (SELECT COUNT(*)::int FROM gis.maps) as maps_count,
        (SELECT COUNT(*)::int FROM gis.ogc_service) as ogc_count
    `;

    const result = await pg.query(statsQuery);
    const stats = result?.rows?.[0] || {};

    return {
      cards: {
        services: stats.services_count || 0,
        registers: stats.registers_count || 0,
        maps: stats.maps_count || 0,
        ogc: stats.ogc_count || 0,
      },
    };
  }
  catch (error) {
    console.error('Dashboard error:', error);
    return { status: 500, error: 'Internal Server Error' };
  }
}
