import express from 'express';
import {
  getTournaments,
  addTournament,
  updateTournament,
  addParticipant,
  getParticipants,
  setModus,
  createGroups,
  fillGroups,
  createGroupMatches,
  getGroups,
  getTournament,
  getTournamentMatches,
  addMatchResult,
  finishMatch,
  setMatchTable,
  startKnockout,
  manualAssignGroups,
  resetGroups,
  resetMatches,
  removeParticipant,
  updateParticipantSeeded,
  deleteMatchResult,
  reopenMatch,
  deleteKnockoutMatches,
  setMatchActive,
  addExternalParticipant,
  getExternalParticipants,
  removeExternalParticipant,
  updateExternalParticipantSeeded,
  getTournamentClasses,
  addTournamentClass,
  updateTournamentClass,
  deleteTournamentClass,
  updateParticipantClass,
  createGroupsPerClass,
  assignParticipantToGroup,
  getPairings,
  createPairing,
  updatePairing,
  deletePairing,
  distributeTables,
} from '../controllers/tournamentController.js';
import {
  getStages,
  upsertStages,
  advanceStage,
} from '../controllers/tournamentStagesController.js';
import { authenticate } from '../middleware/authMiddleware.js';

const router = express.Router();

router.post('/participant', authenticate, addParticipant);
router.post('/participants', authenticate, getParticipants);
router.delete('/participant', authenticate, removeParticipant);
router.put('/participant/:clubId/:tournamentId/:participantId/seeded', authenticate, updateParticipantSeeded);
router.post('/modus', authenticate, setModus);
router.post('/groups/reset', authenticate, resetGroups);
router.post('/matches/reset', authenticate, resetMatches);
router.put('/groups', authenticate, createGroups);
router.post('/groups/create', authenticate, createGroupsPerClass);
router.post('/groups', authenticate, fillGroups);
router.post('/matches/create', authenticate, createGroupMatches);
router.get('/groups', authenticate, getGroups);
router.post('/match/result', authenticate, addMatchResult);
router.delete('/match/result', authenticate, deleteMatchResult);
router.post("/match/reopen", authenticate, reopenMatch);
router.post('/match/finish', authenticate, finishMatch);
router.put('/match/:clubId/:tournamentId/:matchId/table', authenticate, setMatchTable);
router.put('/match/:clubId/:tournamentId/:matchId/active', authenticate, setMatchActive);
router.get('/matches/:clubId/:tournamentId', authenticate, getTournamentMatches);
router.post('/knockout', authenticate, startKnockout);
router.delete("/matches/knockout", authenticate, deleteKnockoutMatches);
router.post('/groups/manual', authenticate, manualAssignGroups);
router.post('/matches/distribute', authenticate, distributeTables);
router.put('/participant/group', authenticate, assignParticipantToGroup); // Muss VOR /:clubId/:tournamentId stehen!
router.put('/:clubId/:tournamentId', authenticate, updateTournament);
router.get('/:clubId/:tournamentId', authenticate, getTournament);
// Externe Teilnehmer
router.post('/external-participant', authenticate, addExternalParticipant);
router.post('/external-participants', authenticate, getExternalParticipants);
router.delete('/external-participant', authenticate, removeExternalParticipant);
router.put('/external-participant/:clubId/:tournamentId/:participantId/seeded', authenticate, updateExternalParticipantSeeded);

// Tournament Classes
router.get('/classes/:clubId/:tournamentId', authenticate, getTournamentClasses);
router.post('/class/:clubId/:tournamentId', authenticate, addTournamentClass);
router.put('/class/:clubId/:tournamentId/:classId', authenticate, updateTournamentClass);
router.delete('/class/:clubId/:tournamentId/:classId', authenticate, deleteTournamentClass);
router.put('/participant/:clubId/:tournamentId/:participantId/class', authenticate, updateParticipantClass);

// Tournament Pairings
router.get('/pairings/:clubId/:tournamentId/:classId', authenticate, getPairings);
router.post('/pairing/:clubId/:tournamentId/:classId', authenticate, createPairing);
router.put('/pairing/:clubId/:tournamentId/:pairingId', authenticate, updatePairing);
router.delete('/pairing/:clubId/:tournamentId/:pairingId', authenticate, deletePairing);

// Tournament Stages (mehrere Runden)
router.get('/stages', authenticate, getStages);
router.put('/stages', authenticate, upsertStages);
router.post('/stages/advance', authenticate, advanceStage);

// Muss NACH allen festen Pfaden stehen, sonst matcht z.B. '/stages' als clubId='stages'
router.get('/:clubId', authenticate, getTournaments);
router.post('/', authenticate, addTournament);

export default router;
