import Fastify from 'fastify';
import cors from '@fastify/cors';
import websocket from '@fastify/websocket';
import { priceRoutes } from './routes/prices';
import { indexRoutes } from './routes/index';
import { websocketRoutes } from './routes/websocket';

const server = Fastify({
  logger: {
    level: process.env.LOG_LEVEL || 'info',
  },
});

// Register plugins
server.register(cors, {
  origin: true,
});

server.register(websocket);

// Register routes
server.register(priceRoutes, { prefix: '/api/prices' });
server.register(indexRoutes, { prefix: '/api/index' });
server.register(websocketRoutes);

// Health check
server.get('/health', async () => {
  return { status: 'ok', timestamp: new Date().toISOString() };
});

// Test endpoint to trigger scrape
server.get('/api/test/scrape', async () => {
  return { message: 'Scrape will be triggered by scheduler' };
});

export default server;
