import express from 'express';
import bodyParser from 'body-parser';

export function createStoreServer(port = 3333) {
    const app = express();
    const store = new Map();

    app.use(bodyParser.json());

    // Set value
    app.post('/set/:key', (req, res) => {
        const { key } = req.params;
        const { value } = req.body;
        store.set(key, value);
        res.json({ success: true });
    });

    // Get value
    app.get('/get/:key', (req, res) => {
        const { key } = req.params;
        if (store.has(key)) {
            res.json({ value: store.get(key) });
        } else {
            res.status(404).json({ error: 'Key not found' });
        }
    });

    // Delete value
    app.delete('/delete/:key', (req, res) => {
        const { key } = req.params;
        store.delete(key);
        res.json({ success: true });
    });

    // Clear store
    app.post('/clear', (req, res) => {
        store.clear();
        res.json({ success: true });
    });

    const server = app.listen(port, () => {
        console.log(`Store server listening at http://localhost:${port}`);
    });

    return server;
} 