/**
 * Test Data Database Server
 * Express.js server with search API and web interface
 */

const express = require('express');
const cors = require('cors');
const path = require('path');

const apiRoutes = require('./routes/api');

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

// Middleware
app.use(cors());
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));

// API routes
app.use('/api', apiRoutes);

// Serve index.html for root
app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, 'public', 'index.html'));
});

// Start server - bind to 0.0.0.0 for LAN access
const HOST = '0.0.0.0';
app.listen(PORT, HOST, () => {
    console.log(`\n========================================`);
    console.log(`Test Data Database Server`);
    console.log(`========================================`);
    console.log(`Server running on all interfaces (${HOST}:${PORT})`);
    console.log(`Local:   http://localhost:${PORT}`);
    console.log(`LAN:     http://192.168.0.6:${PORT}`);
    console.log(`API endpoints:`);
    console.log(`  GET /api/search?serial=...&model=...`);
    console.log(`  GET /api/record/:id`);
    console.log(`  GET /api/datasheet/:id`);
    console.log(`  GET /api/stats`);
    console.log(`  GET /api/export?format=csv&...`);
    console.log(`========================================\n`);
});

module.exports = app;

