import express from 'express';
import cors from 'cors';
import { Ollama } from 'ollama';

const ollama = new Ollama();

async function chooseLayout(prefs) {
  const prompt = `
    The user describes their preferred way of working: "${prefs}".
Decide whether they would prefer a "card" layout (visual, spacious)
or a "list" layout (compact, information-dense).
Respond ONLY with "card" or "list".

  `;

  const result = await ollama.generate({ model: 'llama3.2:1b', prompt });

  const answer = result.response.trim().toLowerCase();

  console.log(answer);

  return answer === 'card' ? 'card' : 'list';
}

const app = express();
app.use(express.json());
app.use(cors());

app.post('/ui-personalize', async (req, res) => {
  const { prefs } = req.body;
  const layout = await chooseLayout(prefs);
  res.json({ layout });
});

app.listen(3001, () => console.log('Backend running on http://localhost:3001'));
