const express = require('express');
const cors = require('cors');
const path = require('path');
const app = express();
const families = require('./data/families.json');

// Enable CORS for frontend
app.use(cors());

// Serve static files from the public directory (for standalone mode)
if (process.env.SERVE_STATIC === 'true') {
  app.use(express.static(path.join(__dirname, '../public')));
}

// API endpoint for family search
app.get('/api/search', (req, res) => {
  const query = req.query.family?.toLowerCase();
  if (!query) {
    return res.json([]);
  }
  const matches = families.filter(fam => fam.family.toLowerCase().includes(query));
  res.json(matches);
});

// Health check endpoint
app.get('/api/health', (req, res) => {
  res.json({ status: 'ok', timestamp: new Date().toISOString() });
});

const port = process.env.PORT || 3000;
app.listen(port, '0.0.0.0', () => {
  console.log(`🗺️  Ora Map Backend API running at http://localhost:${port}`);
  console.log(`📍 Search endpoint: http://localhost:${port}/api/search`);
  console.log(`💚 Health endpoint: http://localhost:${port}/api/health`);
});
