package de.szut.casino.dice;

import de.szut.casino.exceptionHandling.exceptions.InsufficientFundsException;
import de.szut.casino.exceptionHandling.exceptions.UserNotFoundException;
import de.szut.casino.shared.service.BalanceService;
import de.szut.casino.user.UserEntity;
import de.szut.casino.user.UserService;
import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

@RestController
public class DiceController {
    private final UserService userService;
    private final BalanceService balanceService;
    private final DiceService diceService;

    public DiceController(UserService userService, BalanceService balanceService, DiceService diceService) {
        this.userService = userService;
        this.balanceService = balanceService;
        this.diceService = diceService;
    }

    @PostMapping("/dice")
    public ResponseEntity<Object> rollDice(@RequestBody @Valid DiceDto diceDto) {
        UserEntity user = userService.getCurrentUser();

        if (!this.balanceService.hasFunds(user, diceDto)) {
            throw new InsufficientFundsException();
        }

        return ResponseEntity.ok(diceService.play(user, diceDto));
    }
}
