validate([ 'round_number' => 'required|integer|min:1', 'name' => 'nullable|string|max:50', ]); // Check if user owns the video if (Auth::id() !== $video->user_id) { return response()->json(['success' => false, 'message' => 'Unauthorized'], 403); } $round = MatchRound::create([ 'video_id' => $video->id, 'round_number' => $request->round_number, 'name' => $request->name ?? 'ROUND '.$request->round_number, ]); return response()->json([ 'success' => true, 'round' => $round, 'message' => 'Round added successfully!', ]); } public function updateRound(Request $request, MatchRound $round) { $request->validate([ 'name' => 'required|string|max:50', ]); // Check if user owns the video if (Auth::id() !== $round->video->user_id) { return response()->json(['success' => false, 'message' => 'Unauthorized'], 403); } $round->update([ 'name' => $request->name, ]); return response()->json([ 'success' => true, 'round' => $round, 'message' => 'Round updated successfully!', ]); } public function destroyRound(MatchRound $round) { // Check if user owns the video if (Auth::id() !== $round->video->user_id) { return response()->json(['success' => false, 'message' => 'Unauthorized'], 403); } $round->delete(); return response()->json([ 'success' => true, 'message' => 'Round deleted successfully!', ]); } // ==================== POINTS ==================== public function storePoint(Request $request, Video $video) { $request->validate([ 'round_id' => 'required|exists:match_rounds,id', 'timestamp_seconds' => 'required|integer|min:0', 'action' => 'required|string|max:255', 'points' => 'required|integer|min:1', 'competitor' => 'required|in:blue,red', 'notes' => 'nullable|string|max:500', ]); // Check if user owns the video if (Auth::id() !== $video->user_id) { return response()->json(['success' => false, 'message' => 'Unauthorized'], 403); } // Calculate scores $previousPoints = MatchPoint::where('video_id', $video->id) ->where('match_round_id', $request->round_id) ->where('timestamp_seconds', '<', $request->timestamp_seconds) ->orderBy('timestamp_seconds', 'desc') ->first(); $scoreBlue = $previousPoints ? $previousPoints->score_blue : 0; $scoreRed = $previousPoints ? $previousPoints->score_red : 0; if ($request->competitor === 'blue') { $scoreBlue += $request->points; } else { $scoreRed += $request->points; } $point = MatchPoint::create([ 'video_id' => $video->id, 'match_round_id' => $request->round_id, 'timestamp_seconds' => $request->timestamp_seconds, 'action' => $request->action, 'points' => $request->points, 'competitor' => $request->competitor, 'notes' => $request->notes, 'score_blue' => $scoreBlue, 'score_red' => $scoreRed, ]); return response()->json([ 'success' => true, 'point' => $point, 'message' => 'Point added successfully!', ]); } public function updatePoint(Request $request, MatchPoint $point) { $request->validate([ 'timestamp_seconds' => 'required|integer|min:0', 'action' => 'required|string|max:255', 'points' => 'required|integer|min:1', 'competitor' => 'required|in:blue,red', 'notes' => 'nullable|string|max:500', ]); // Check if user owns the video if (Auth::id() !== $point->video->user_id) { return response()->json(['success' => false, 'message' => 'Unauthorized'], 403); } $point->update([ 'timestamp_seconds' => $request->timestamp_seconds, 'action' => $request->action, 'points' => $request->points, 'competitor' => $request->competitor, 'notes' => $request->notes, ]); return response()->json([ 'success' => true, 'point' => $point, 'message' => 'Point updated successfully!', ]); } public function destroyPoint(MatchPoint $point) { // Check if user owns the video if (Auth::id() !== $point->video->user_id) { return response()->json(['success' => false, 'message' => 'Unauthorized'], 403); } $point->delete(); return response()->json([ 'success' => true, 'message' => 'Point deleted successfully!', ]); } // ==================== COACH REVIEWS ==================== public function storeReview(Request $request, Video $video) { $request->validate([ 'start_time_seconds' => 'required|integer|min:0', 'end_time_seconds' => 'nullable|integer|min:0', 'note' => 'required|string|max:1000', 'coach_name' => 'required|string|max:100', 'emoji' => 'nullable|string|max:10', ]); // Check if user owns the video if (Auth::id() !== $video->user_id) { return response()->json(['success' => false, 'message' => 'Unauthorized'], 403); } $review = CoachReview::create([ 'video_id' => $video->id, 'user_id' => Auth::id(), 'start_time_seconds' => $request->start_time_seconds, 'end_time_seconds' => $request->end_time_seconds, 'note' => $request->note, 'coach_name' => $request->coach_name, 'emoji' => $request->emoji ?? '🔥', ]); return response()->json([ 'success' => true, 'review' => $review, 'message' => 'Coach note added successfully!', ]); } public function updateReview(Request $request, CoachReview $review) { $request->validate([ 'start_time_seconds' => 'required|integer|min:0', 'end_time_seconds' => 'nullable|integer|min:0', 'note' => 'required|string|max:1000', 'coach_name' => 'required|string|max:100', 'emoji' => 'nullable|string|max:10', ]); // Check if user owns the video if (Auth::id() !== $review->video->user_id) { return response()->json(['success' => false, 'message' => 'Unauthorized'], 403); } $review->update([ 'start_time_seconds' => $request->start_time_seconds, 'end_time_seconds' => $request->end_time_seconds, 'note' => $request->note, 'coach_name' => $request->coach_name, 'emoji' => $request->emoji, ]); return response()->json([ 'success' => true, 'review' => $review, 'message' => 'Coach note updated successfully!', ]); } public function destroyReview(CoachReview $review) { // Check if user owns the video if (Auth::id() !== $review->video->user_id) { return response()->json(['success' => false, 'message' => 'Unauthorized'], 403); } $review->delete(); return response()->json([ 'success' => true, 'message' => 'Coach note deleted successfully!', ]); } // ==================== GET DATA ==================== public function getMatchData(Video $video) { // Check if user can view this video if (! $video->canView(Auth::user())) { return response()->json(['success' => false, 'message' => 'Unauthorized'], 403); } $rounds = MatchRound::where('video_id', $video->id) ->with('points') ->orderBy('round_number') ->get(); $reviews = CoachReview::where('video_id', $video->id) ->orderBy('start_time_seconds') ->get(); return response()->json([ 'success' => true, 'rounds' => $rounds, 'reviews' => $reviews, ]); } }