import { Router } from 'express';
import pool from '../db.js';
import { requireAuth } from '../middleware/auth.js';

const router = Router();

// Score a match (admin)
router.post('/:id/score', requireAuth, async (req,res)=>{
  const mid = Number(req.params.id);
  const { score_a, score_b, done } = req.body || {};
  if (!mid) return res.status(400).json({ error:'bad_match_id' });
  const { rows } = await pool.query(
    'update matches set score_a=$2, score_b=$3, done=coalesce($4, done) where id=$1 returning *',
    [mid, score_a ?? 0, score_b ?? 0, done]
  );
  if (!rows[0]) return res.status(404).json({ error:'match_not_found' });
  res.json(rows[0]);
});

export default router;
