"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', auth_js_1.authenticate, async (req, res) => { try { const { gameId } = req.params; const game = await index_js_1.prisma.game.findUnique({ where: { id: gameId }, include: { legs: { orderBy: { sequenceNumber: 'asc' } } } }); if (!game) { return res.status(404).json({ error: 'Game not found' }); } if (game.gameMasterId !== req.user.id) { return res.status(403).json({ error: 'Not authorized' }); } res.json(game.legs); } catch (error) { console.error('Get legs error:', error); res.status(500).json({ error: 'Failed to get legs' }); } }); router.post('/game/:gameId', auth_js_1.authenticate, async (req, res) => { try { const { gameId } = req.params; const { description, conditionType, conditionDetails, locationLat, locationLng, timeLimit } = req.body; const game = await index_js_1.prisma.game.findUnique({ where: { id: gameId }, include: { legs: true } }); if (!game) { return res.status(404).json({ error: 'Game not found' }); } if (game.gameMasterId !== req.user.id) { return res.status(403).json({ error: 'Not authorized' }); } const maxSequence = game.legs.reduce((max, leg) => Math.max(max, leg.sequenceNumber), 0); const leg = await index_js_1.prisma.leg.create({ data: { gameId, sequenceNumber: maxSequence + 1, description, conditionType: conditionType || 'photo', conditionDetails, locationLat, locationLng, timeLimit } }); res.json(leg); } catch (error) { console.error('Create leg error:', error); res.status(500).json({ error: 'Failed to create leg' }); } }); router.put('/:legId', auth_js_1.authenticate, async (req, res) => { try { const { legId } = req.params; const { description, conditionType, conditionDetails, locationLat, locationLng, timeLimit } = req.body; const leg = await index_js_1.prisma.leg.findUnique({ where: { id: legId }, include: { game: true } }); if (!leg) { return res.status(404).json({ error: 'Leg not found' }); } if (leg.game.gameMasterId !== req.user.id) { return res.status(403).json({ error: 'Not authorized' }); } const updated = await index_js_1.prisma.leg.update({ where: { id: legId }, data: { description, conditionType, conditionDetails, locationLat, locationLng, timeLimit } }); res.json(updated); } catch (error) { console.error('Update leg error:', error); res.status(500).json({ error: 'Failed to update leg' }); } }); router.delete('/:legId', auth_js_1.authenticate, async (req, res) => { try { const { legId } = req.params; const leg = await index_js_1.prisma.leg.findUnique({ where: { id: legId }, include: { game: true } }); if (!leg) { return res.status(404).json({ error: 'Leg not found' }); } if (leg.game.gameMasterId !== req.user.id) { return res.status(403).json({ error: 'Not authorized' }); } await index_js_1.prisma.leg.delete({ where: { id: legId } }); res.json({ message: 'Leg deleted' }); } catch (error) { console.error('Delete leg error:', error); res.status(500).json({ error: 'Failed to delete leg' }); } }); router.post('/:legId/photo', auth_js_1.authenticate, async (req, res) => { try { const { legId } = req.params; const { teamId, photoUrl } = req.body; if (!teamId || !photoUrl) { return res.status(400).json({ error: 'Team ID and photo URL are required' }); } const leg = await index_js_1.prisma.leg.findUnique({ where: { id: legId }, include: { game: true } }); if (!leg) { return res.status(404).json({ error: 'Leg not found' }); } const team = await index_js_1.prisma.team.findUnique({ where: { id: teamId } }); if (!team || team.gameId !== leg.gameId) { return res.status(403).json({ error: 'Team not in this game' }); } const submission = await index_js_1.prisma.photoSubmission.create({ data: { teamId, legId, photoUrl } }); res.json(submission); } catch (error) { console.error('Submit photo error:', error); res.status(500).json({ error: 'Failed to submit photo' }); } }); exports.default = router;