/**
 * audit-svc — Enterprise audit logs
 * docs/architecture: 01-perplexity-analogue-design.md §5.12, §2.2.N
 * API: GET /api/v1/admin/audit-logs?from=&to=&userId=
 */

import Fastify from 'fastify';
import cors from '@fastify/cors';

const PORT = parseInt(process.env.PORT ?? '3012', 10);

const app = Fastify({ logger: true });

await app.register(cors, { origin: true });

app.get('/health', async () => ({ status: 'ok' }));
app.get('/ready', async () => ({ status: 'ready' }));
app.get('/metrics', async (_req, reply) => {
  reply.header('Content-Type', 'text/plain; charset=utf-8');
  return reply.send(
    '# HELP gooseek_up Service is up (1) or down (0)\n' +
    '# TYPE gooseek_up gauge\n' +
    'gooseek_up 1\n'
  );
});

app.get<{
  Querystring: { from?: string; to?: string; userId?: string };
}>('/api/v1/admin/audit-logs', async (req, reply) => {
  return reply.send({
    items: [],
    _stub: true,
    message: 'Audit logs require Enterprise. Configure POSTGRES_URL and enable logging.',
  });
});

try {
  await app.listen({ port: PORT, host: '0.0.0.0' });
  console.log('audit-svc listening on :' + PORT);
} catch (err) {
  console.error(err);
  process.exit(1);
}
