// PRE Web GUI — Session & Project routes
const { Router } = require('express');
const {
  listSessions, getSession, createSession, deleteSession,
  renameSession, rewindSession,
  listProjects, createProject, renameProject, deleteProject, moveSessionToProject,
} = require('../sessions');
const { clearSessionTier } = require('../tools');

const router = Router();

// ── Sessions ──

router.get('/sessions', (_req, res) => {
  res.json(listSessions());
});

router.get('/sessions/search', (req, res) => {
  const fts = require('../fts');
  const query = req.query.q || req.query.query || '';
  if (!query) return res.status(400).json({ error: 'Query parameter q is required' });
  const result = fts.searchSessions(query, {
    maxResults: parseInt(req.query.count) || 20,
    project: req.query.project || null,
  });
  res.json({ results: result });
});

router.get('/sessions/:id', (req, res) => {
  const messages = getSession(decodeURIComponent(req.params.id));
  res.json(messages);
});

router.post('/sessions/new', (req, res) => {
  const { project, channel, projectSlug } = req.body || {};
  const id = createSession(project || 'web', channel || 'general', true, projectSlug || null);
  res.json({ id });
});

router.delete('/sessions/:id', (req, res) => {
  const id = decodeURIComponent(req.params.id);
  const ok = deleteSession(id);
  if (!ok) return res.status(404).json({ error: 'Session not found' });
  clearSessionTier(id);
  res.json({ deleted: id });
});

router.post('/sessions/:id/rename', (req, res) => {
  const id = decodeURIComponent(req.params.id);
  const { name } = req.body || {};
  renameSession(id, name);
  res.json({ id, name: name || null });
});

router.post('/rewind', (req, res) => {
  const { sessionId, turns } = req.body || {};
  if (!sessionId) return res.status(400).json({ error: 'sessionId required' });
  const remaining = rewindSession(sessionId, turns || 1);
  res.json({ remaining: remaining.length });
});

// ── Projects ──

router.get('/projects', (_req, res) => {
  res.json(listProjects());
});

router.post('/projects', (req, res) => {
  const { name } = req.body || {};
  if (!name || !name.trim()) return res.status(400).json({ error: 'name required' });
  const project = createProject(name);
  res.json(project);
});

router.post('/projects/:slug/rename', (req, res) => {
  const slug = decodeURIComponent(req.params.slug);
  const { name } = req.body || {};
  if (!name || !name.trim()) return res.status(400).json({ error: 'name required' });
  const ok = renameProject(slug, name);
  if (!ok) return res.status(404).json({ error: 'Project not found' });
  res.json({ slug, name: name.trim() });
});

router.delete('/projects/:slug', (req, res) => {
  const slug = decodeURIComponent(req.params.slug);
  const ok = deleteProject(slug);
  if (!ok) return res.status(404).json({ error: 'Project not found' });
  res.json({ deleted: slug });
});

router.post('/sessions/:id/move', (req, res) => {
  const id = decodeURIComponent(req.params.id);
  const { projectSlug } = req.body || {};
  moveSessionToProject(id, projectSlug || null);
  res.json({ id, projectSlug: projectSlug || null });
});

module.exports = router;
