"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const express_1 = require("express"); const index_js_1 = require("../index.js"); const auth_js_1 = require("../middleware/auth.js"); const router = (0, express_1.Router)(); router.get('/game/:gameId', async (req, res) => { try { const { gameId } = req.params; const teams = await index_js_1.prisma.team.findMany({ where: { gameId }, include: { members: { include: { user: { select: { id: true, name: true, email: true } } } }, captain: { select: { id: true, name: true } }, currentLeg: true }, orderBy: { createdAt: 'asc' } }); res.json(teams); } catch (error) { console.error('Get teams error:', error); res.status(500).json({ error: 'Failed to get teams' }); } }); router.post('/game/:gameId', auth_js_1.authenticate, async (req, res) => { try { const { gameId } = req.params; const { name } = req.body; const game = await index_js_1.prisma.game.findUnique({ where: { id: gameId }, include: { teams: true } }); if (!game) { return res.status(404).json({ error: 'Game not found' }); } if (game.status !== 'DRAFT' && game.status !== 'LIVE') { return res.status(400).json({ error: 'Cannot join game at this time' }); } const existingMember = await index_js_1.prisma.teamMember.findFirst({ where: { userId: req.user.id, team: { gameId } } }); if (existingMember) { return res.status(400).json({ error: 'Already in a team for this game' }); } const team = await index_js_1.prisma.team.create({ data: { gameId, name, captainId: req.user.id } }); await index_js_1.prisma.teamMember.create({ data: { teamId: team.id, userId: req.user.id } }); const created = await index_js_1.prisma.team.findUnique({ where: { id: team.id }, include: { members: { include: { user: { select: { id: true, name: true, email: true } } } }, captain: { select: { id: true, name: true } } } }); res.json(created); } catch (error) { console.error('Create team error:', error); res.status(500).json({ error: 'Failed to create team' }); } }); router.post('/:teamId/join', auth_js_1.authenticate, async (req, res) => { try { const { teamId } = req.params; const team = await index_js_1.prisma.team.findUnique({ where: { id: teamId }, include: { game: true, members: true } }); if (!team) { return res.status(404).json({ error: 'Team not found' }); } if (team.members.length >= 5) { return res.status(400).json({ error: 'Team is full (max 5 members)' }); } const existingMember = await index_js_1.prisma.teamMember.findFirst({ where: { userId: req.user.id, teamId } }); if (existingMember) { return res.status(400).json({ error: 'Already in this team' }); } const gameMember = await index_js_1.prisma.teamMember.findFirst({ where: { userId: req.user.id, team: { gameId: team.gameId } } }); if (gameMember) { return res.status(400).json({ error: 'Already in another team for this game' }); } await index_js_1.prisma.teamMember.create({ data: { teamId, userId: req.user.id } }); res.json({ message: 'Joined team successfully' }); } catch (error) { console.error('Join team error:', error); res.status(500).json({ error: 'Failed to join team' }); } }); router.post('/:teamId/leave', auth_js_1.authenticate, async (req, res) => { try { const { teamId } = req.params; const team = await index_js_1.prisma.team.findUnique({ where: { id: teamId } }); if (!team) { return res.status(404).json({ error: 'Team not found' }); } if (team.captainId === req.user.id) { return res.status(400).json({ error: 'Captain cannot leave the team' }); } await index_js_1.prisma.teamMember.deleteMany({ where: { teamId, userId: req.user.id } }); res.json({ message: 'Left team successfully' }); } catch (error) { console.error('Leave team error:', error); res.status(500).json({ error: 'Failed to leave team' }); } }); router.post('/:teamId/advance', auth_js_1.authenticate, async (req, res) => { try { const { teamId } = req.params; const team = await index_js_1.prisma.team.findUnique({ where: { id: teamId }, include: { game: { include: { legs: { orderBy: { sequenceNumber: 'asc' } } } }, currentLeg: true } }); if (!team) { return res.status(404).json({ error: 'Team not found' }); } if (team.game.gameMasterId !== req.user.id) { return res.status(403).json({ error: 'Not authorized' }); } const legs = team.game.legs; const currentLeg = team.currentLeg; let nextLeg = null; if (currentLeg) { const currentIndex = legs.findIndex((l) => l.id === currentLeg.id); if (currentIndex < legs.length - 1) { nextLeg = legs[currentIndex + 1]; } } else if (legs.length > 0) { nextLeg = legs[0]; } const updated = await index_js_1.prisma.team.update({ where: { id: teamId }, data: { currentLegId: nextLeg?.id || null, status: nextLeg ? 'ACTIVE' : 'FINISHED' }, include: { members: { include: { user: { select: { id: true, name: true } } } }, currentLeg: true } }); res.json(updated); } catch (error) { console.error('Advance team error:', error); res.status(500).json({ error: 'Failed to advance team' }); } }); router.post('/:teamId/deduct', auth_js_1.authenticate, async (req, res) => { try { const { teamId } = req.params; const { seconds } = req.body; const team = await index_js_1.prisma.team.findUnique({ where: { id: teamId }, include: { game: true } }); if (!team) { return res.status(404).json({ error: 'Team not found' }); } if (team.game.gameMasterId !== req.user.id) { return res.status(403).json({ error: 'Not authorized' }); } const deduction = seconds || team.game.timeDeductionPenalty || 60; const updated = await index_js_1.prisma.team.update({ where: { id: teamId }, data: { totalTimeDeduction: { increment: deduction } } }); res.json(updated); } catch (error) { console.error('Deduct time error:', error); res.status(500).json({ error: 'Failed to deduct time' }); } }); router.post('/:teamId/disqualify', auth_js_1.authenticate, async (req, res) => { try { const { teamId } = req.params; const team = await index_js_1.prisma.team.findUnique({ where: { id: teamId }, include: { game: true } }); if (!team) { return res.status(404).json({ error: 'Team not found' }); } if (team.game.gameMasterId !== req.user.id) { return res.status(403).json({ error: 'Not authorized' }); } const updated = await index_js_1.prisma.team.update({ where: { id: teamId }, data: { status: 'DISQUALIFIED' } }); res.json(updated); } catch (error) { console.error('Disqualify team error:', error); res.status(500).json({ error: 'Failed to disqualify team' }); } }); router.post('/:teamId/location', auth_js_1.authenticate, async (req, res) => { try { const { teamId } = req.params; const { lat, lng } = req.body; const team = await index_js_1.prisma.team.findUnique({ where: { id: teamId } }); if (!team) { return res.status(404).json({ error: 'Team not found' }); } const member = await index_js_1.prisma.teamMember.findFirst({ where: { teamId, userId: req.user.id } }); if (!member && team.captainId !== req.user.id) { return res.status(403).json({ error: 'Not authorized' }); } const updated = await index_js_1.prisma.team.update({ where: { id: teamId }, data: { lat, lng } }); res.json(updated); } catch (error) { console.error('Update location error:', error); res.status(500).json({ error: 'Failed to update location' }); } }); router.get('/:teamId', auth_js_1.authenticate, async (req, res) => { try { const { teamId } = req.params; const team = await index_js_1.prisma.team.findUnique({ where: { id: teamId }, include: { members: { include: { user: { select: { id: true, name: true, email: true } } } }, captain: { select: { id: true, name: true } }, currentLeg: true, game: { include: { legs: { orderBy: { sequenceNumber: 'asc' } } } } } }); if (!team) { return res.status(404).json({ error: 'Team not found' }); } res.json(team); } catch (error) { console.error('Get team error:', error); res.status(500).json({ error: 'Failed to get team' }); } }); exports.default = router;