import { Router } from 'express';
import { saveState } from '../services/SaveState.js';

const router = Router();

const VALID_PORTRAITS = [
  'player-silhouette',
  'player-01',
  'player-02',
  'player-03',
  'player-04',
  'player-05'
];

router.get('/', (_req, res) => {
  const state = saveState.get();
  res.json({ portrait: state.player_portrait ?? 'player-silhouette' });
});

router.put('/', (req, res) => {
  const { portrait } = req.body ?? {};

  if (!portrait || !VALID_PORTRAITS.includes(portrait)) {
    return res.status(400).json({ error: 'Invalid portrait', valid: VALID_PORTRAITS });
  }

  saveState.set({ player_portrait: portrait });
  res.json({ portrait });
});

export default router;
