import express from 'express';
import { createBullBoard } from '@bull-board/api';
import { BullAdapter } from '@bull-board/api/bullAdapter';
import { ExpressAdapter } from '@bull-board/express';
import { crawlerQueue } from './queues/crawlerQueue';
import { crawlerRoutes } from './routes/crawlerRoutes';
import { logger } from '@cloneweb/shared';

const app = express();
const PORT = process.env.PORT || 3001;

// Middleware
app.use(express.json());

// Bull Board for queue monitoring
const serverAdapter = new ExpressAdapter();
serverAdapter.setBasePath('/admin/queues');

const { addQueue } = createBullBoard({
  queues: [new BullAdapter(crawlerQueue)],
  serverAdapter: serverAdapter,
});

app.use('/admin/queues', serverAdapter.getRouter());

// Routes
app.use('/api/crawler', crawlerRoutes);

// Health check
app.get('/health', (req, res) => {
  res.json({ status: 'ok', service: 'crawler-service' });
});

app.listen(PORT, () => {
  logger.info(`Crawler service running on port ${PORT}`);
});

export default app;