FarmerController.java

package edu.ucsb.cs156.happiercows.controllers;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import edu.ucsb.cs156.happiercows.repositories.FarmerRepository;
import edu.ucsb.cs156.happiercows.repositories.GameRepository;
import edu.ucsb.cs156.happiercows.entities.User;
import edu.ucsb.cs156.happiercows.entities.Farmer;
import edu.ucsb.cs156.happiercows.entities.FarmerActivity;
import edu.ucsb.cs156.happiercows.entities.Game;
import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException;
import edu.ucsb.cs156.happiercows.errors.NoCowsException;
import edu.ucsb.cs156.happiercows.errors.NotEnoughMoneyException;
import edu.ucsb.cs156.happiercows.errors.GameHiddenException;
import edu.ucsb.cs156.happiercows.errors.NotEnrolledInCourseAssociatedWithGameException;
import edu.ucsb.cs156.happiercows.models.FarmerWithStudentStatus;
import edu.ucsb.cs156.happiercows.services.CourseAccessService;
import edu.ucsb.cs156.happiercows.services.FarmerActivityService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.transaction.annotation.Transactional;

import edu.ucsb.cs156.happiercows.entities.CowHerd;
import edu.ucsb.cs156.happiercows.repositories.CowHerdRepository;



@Tag(name = "User Game")
@RequestMapping("/api/farmer")
@RestController
public class FarmerController extends ApiController {

  @Autowired
  private FarmerRepository farmerRepository;


  @Autowired
  private CowHerdRepository cowHerdRepository;

  @Autowired
  private GameRepository gameRepository;

  @Autowired
  ObjectMapper mapper;

  @Autowired
  private CourseAccessService courseAccessService;

  @Autowired
  private FarmerActivityService farmerActivityService;

  @Operation(summary = "Get a specific user game (admin only)")
  @PreAuthorize("hasRole('ROLE_ADMIN')")
  @GetMapping("")
  public Farmer getFarmerById(
      @Parameter(name="userId") @RequestParam Long userId,
      @Parameter(name="gameId") @RequestParam Long gameId) throws JsonProcessingException {

    Farmer farmer = farmerRepository.findByGameIdAndUserId(gameId, userId)
        .orElseThrow(
            () -> new EntityNotFoundException(Farmer.class, "gameId", gameId, "userId", userId));
    return farmer;
  }

  @Operation(summary = "Get a user game for current user")
  @PreAuthorize("hasRole('ROLE_USER')")
  @GetMapping("/forcurrentuser")
  public Farmer getFarmerById(
      @Parameter(name="gameId") @RequestParam Long gameId) throws JsonProcessingException {

    User u = getCurrentUser().getUser();
    Long userId = u.getId();
    Game game = gameRepository.findById(gameId)
        .orElseThrow(() -> new EntityNotFoundException("Game", gameId));
    ensureUserCanAccessCourseLinkedGame(u, game);
    Farmer farmer = farmerRepository.findByGameIdAndUserId(gameId, userId)
        .orElseThrow(
            () -> new EntityNotFoundException(Farmer.class, "gameId", gameId, "userId", userId));
    return farmer;
  }

  @Operation(summary = "Get all herds for the current farmer")
  @PreAuthorize("hasRole('ROLE_USER')")
  @GetMapping("/herds")
  public List<Map<String, Object>> getFarmerHerds(@RequestParam Long gameId) {
    User user = getCurrentUser().getUser();
    Long userId = user.getId();

    Game game = gameRepository.findById(gameId)
        .orElseThrow(() -> new EntityNotFoundException("Game", gameId));

    ensureUserCanAccessCourseLinkedGame(user, game);

    if (game.isHidden()) {
      throw new GameHiddenException(gameId);
    }

    farmerRepository.findByGameIdAndUserId(gameId, userId)
        .orElseThrow(() -> new EntityNotFoundException(
            Farmer.class, "gameId", gameId, "userId", userId));

    List<Map<String, Object>> herds = new ArrayList<>();

    cowHerdRepository.findByFarmer_Id_UserIdAndFarmer_Id_GameId(userId, gameId)
        .forEach(herd -> herds.add(Map.of(
            "id", herd.getId(),
            "numOfCows", herd.getNumOfCows(),
            "cowHealth", herd.getCowHealth()
        )));

    return herds;
  }


  @Operation(summary = "Buy a cow, totalWealth updated")
  @PreAuthorize("hasRole('ROLE_USER')")
  @PutMapping("/buy")
  public ResponseEntity<String> putFarmerByIdBuy(
          @Parameter(name="gameId") @RequestParam Long gameId,
          @Parameter(name="numCows") @RequestParam int numCows) throws NotEnoughMoneyException, JsonProcessingException{

        User u = getCurrentUser().getUser();
        Long userId = u.getId();

        Game game = gameRepository.findById(gameId).orElseThrow( 
          ()->new EntityNotFoundException("Game", gameId));

        ensureUserCanAccessCourseLinkedGame(u, game);

        if (game.isHidden()) {
          throw new GameHiddenException(gameId);
        }

        Farmer farmer = farmerRepository.findByGameIdAndUserId(gameId, userId)
        .orElseThrow(
            () -> new EntityNotFoundException(Farmer.class, "gameId", gameId, "userId", userId));

        if(farmer.getTotalWealth() >= (game.getCowPrice() * numCows)){
          farmer.setTotalWealth(farmer.getTotalWealth() - (game.getCowPrice() * numCows));
          farmer.setNumOfCows(farmer.getNumOfCows() + numCows);
          farmer.setCowsBought(farmer.getCowsBought() + numCows);
        }
        else{
          throw new NotEnoughMoneyException("You need more money!");
        }
        farmerRepository.save(farmer);
        CowHerd herd = CowHerd.builder()
            .farmer(farmer)
            .numOfCows(numCows)
            .cowHealth(100)
            .build();

        cowHerdRepository.save(herd);

        farmerActivityService.recordActivityIfStudentMatch(
            u, game, farmer, FarmerActivity.ACTIVITY_TYPE_BUY, numCows);

        String body = mapper.writeValueAsString(farmer);
        return ResponseEntity.ok().body(body);
    }

  @Operation(summary = "Sell a cow, totalWealth updated")
  @PreAuthorize("hasRole('ROLE_USER')")
  @PutMapping("/sell")
  public ResponseEntity<String> putFarmerByIdSell(
          @Parameter(name="gameId") @RequestParam Long gameId,
          @Parameter(name="numCows") @RequestParam int numCows) throws NoCowsException, JsonProcessingException {
        User u = getCurrentUser().getUser();
        Long userId = u.getId();

        Game game = gameRepository.findById(gameId).orElseThrow( 
          ()->new EntityNotFoundException("Game", gameId));

        ensureUserCanAccessCourseLinkedGame(u, game);

        if (game.isHidden()) {
          throw new GameHiddenException(gameId);
        }

        Farmer farmer = farmerRepository.findByGameIdAndUserId(gameId, userId)
        .orElseThrow(
            () -> new EntityNotFoundException(Farmer.class, "gameId", gameId, "userId", userId));


        if(farmer.getNumOfCows() >= numCows ){
          double cowValue = game.getCowPrice() * farmer.getCowHealth() / 100;
          farmer.setTotalWealth(farmer.getTotalWealth() + (cowValue * numCows));
          farmer.setNumOfCows(farmer.getNumOfCows() - numCows);
          farmer.setCowsSold(farmer.getCowsSold() + numCows);
        }
        else{
          throw new NoCowsException("You do not have enough cows to sell!");
        }

        farmerRepository.save(farmer);

        farmerActivityService.recordActivityIfStudentMatch(
            u, game, farmer, FarmerActivity.ACTIVITY_TYPE_SELL, numCows);

        String body = mapper.writeValueAsString(farmer);
        return ResponseEntity.ok().body(body);
  }

  @Operation(summary = "Sell cows from a herd")
  @PreAuthorize("hasRole('ROLE_USER')")
  @PutMapping("/sell/herd")
  @Transactional
  public ResponseEntity<String> sellCowsFromHerd(
        @RequestParam Long gameId,
        @RequestParam Long herdId,
        @RequestParam int numCows)
        throws NoCowsException, JsonProcessingException {
    User u = getCurrentUser().getUser();
    Long userId = u.getId();

    Game game = gameRepository.findById(gameId).orElseThrow(
        () -> new EntityNotFoundException("Game", gameId));

    ensureUserCanAccessCourseLinkedGame(u, game);

    if (game.isHidden()) {
        throw new GameHiddenException(gameId);
    }

    Farmer farmer = farmerRepository.findByGameIdAndUserId(gameId, userId)
        .orElseThrow(() -> new EntityNotFoundException(
            Farmer.class, "gameId", gameId, "userId", userId));

    CowHerd herd = cowHerdRepository.findById(herdId)
        .orElseThrow(() -> new EntityNotFoundException("CowHerd", herdId));

    if (herd.getFarmer().getUserId() != userId.longValue()
            || herd.getFarmer().getGameId() != gameId.longValue()) {
        throw new EntityNotFoundException("CowHerd", herdId);
    }

    if (numCows <= 0 || numCows > herd.getNumOfCows()
            || numCows > farmer.getNumOfCows()) {
        throw new NoCowsException("You do not have enough cows to sell!");
    }

    double cowValue = game.getCowPrice() * herd.getCowHealth() / 100;

    farmer.setTotalWealth(farmer.getTotalWealth() + cowValue * numCows);
    farmer.setNumOfCows(farmer.getNumOfCows() - numCows);
    farmer.setCowsSold(farmer.getCowsSold() + numCows);

    herd.setNumOfCows(herd.getNumOfCows() - numCows);

    cowHerdRepository.save(herd);
    farmerRepository.save(farmer);

    farmerActivityService.recordActivityIfStudentMatch(
        u, game, farmer, FarmerActivity.ACTIVITY_TYPE_SELL, numCows);

    String body = mapper.writeValueAsString(farmer);
    return ResponseEntity.ok().body(body);
  }

    @Operation(summary = "Get all user game for a specific game")
    @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')")
    @GetMapping("/game/all")
    public List<FarmerWithStudentStatus> getFarmersByGameId(
        @Parameter(name="gameId") @RequestParam Long gameId) {

      Game game = gameRepository.findById(gameId)
          .orElseThrow(() -> new EntityNotFoundException("Game", gameId));

      List<FarmerWithStudentStatus> result = new ArrayList<>();
      for (Farmer farmer : farmerRepository.findByGameId(gameId)) {
        boolean isStudent = courseAccessService
            .findMatchingStudentForCourseLinkedGame(farmer.getUser(), game)
            .isPresent();

        result.add(FarmerWithStudentStatus.builder()
            .userId(farmer.getUserId())
            .gameId(farmer.getGameId())
            .username(farmer.getUsername())
            .totalWealth(farmer.getTotalWealth())
            .numOfCows(farmer.getNumOfCows())
            .cowHealth(farmer.getCowHealth())
            .cowsBought(farmer.getCowsBought())
            .cowsSold(farmer.getCowsSold())
            .cowDeaths(farmer.getCowDeaths())
            .student(isStudent)
            .build());
      }
      return result;
  }

  private void ensureUserCanAccessCourseLinkedGame(User user, Game game) {
    if (game.getCourseId() != null && !courseAccessService.isEligibleForGame(user, game)) {
      throw new NotEnrolledInCourseAssociatedWithGameException();
    }
  }

}