import { Router } from 'express';
import { requireAuth } from './auth.js';
import {
  listProjectsByUser,
  upsertProject,
  getProjectByName,
  deleteProjectByName,
} from './db.js';

const router = Router();

function isValidName(name) {
  return typeof name === 'string' && name.trim().length > 0 && name.trim().length <= 128;
}

router.get('/', requireAuth, async (req, res) => {
  try {
    const rows = await listProjectsByUser(req.user.id);
    const projects = rows.map((row) => ({
      name: row.name,
      state: JSON.parse(row.state_json),
      updatedAt: row.updated_at,
    }));
    res.json({ projects });
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

router.post('/', requireAuth, async (req, res) => {
  const { name, state } = req.body || {};
  if (!isValidName(name)) return res.status(400).json({ error: 'Invalid project name' });
  if (!state || typeof state !== 'object') return res.status(400).json({ error: 'Invalid project state' });

  try {
    await upsertProject({
      userId: req.user.id,
      name: name.trim(),
      stateJson: JSON.stringify(state),
    });
    res.json({ ok: true });
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

router.get('/:name', requireAuth, async (req, res) => {
  const name = decodeURIComponent(req.params.name || '');
  if (!isValidName(name)) return res.status(400).json({ error: 'Invalid project name' });

  try {
    const row = await getProjectByName({ userId: req.user.id, name: name.trim() });
    if (!row) return res.status(404).json({ error: 'Project not found' });
    res.json({
      name: row.name,
      state: JSON.parse(row.state_json),
      updatedAt: row.updated_at,
    });
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

router.delete('/:name', requireAuth, async (req, res) => {
  const name = decodeURIComponent(req.params.name || '');
  if (!isValidName(name)) return res.status(400).json({ error: 'Invalid project name' });

  try {
    const ok = await deleteProjectByName({ userId: req.user.id, name: name.trim() });
    if (!ok) return res.status(404).json({ error: 'Project not found' });
    res.json({ ok: true });
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

export default router;
