package premium_store.controller.controller;

import org.springframework.web.bind.annotation.*;
import premium_store.WebConfiguration;
import premium_store.controller.DTO.ClientDTO;
import premium_store.service.GameClientService;
import premium_store.service.TankService;

import java.util.List;

@RestController
@RequestMapping(WebConfiguration.REST_API + "/client")
public class GameClientController {
    private final GameClientService gameClientService;
    private final TankService tankService;

    public GameClientController(GameClientService gameClientService, TankService tankService){
        this.gameClientService = gameClientService;
        this.tankService = tankService;
    }

    //аннотация PathVariable связывает значения id из URL и Long id
    @GetMapping("/{id}")
    public ClientDTO getClient(@PathVariable Long id) {
        return new ClientDTO(gameClientService.findClient(id));
    }

    //с помощью Java Stream преобразуем набор пришедших данных в объекты StudentDto
    @GetMapping("/")
    public List<ClientDTO> getClients() {
        return gameClientService.findAllClients().stream()
                .map(ClientDTO::new)
                .toList();
    }

    @PostMapping("/")
    public ClientDTO createClient(@RequestParam("nickName") String nickName,
                                  @RequestParam("email") String email,
                                  @RequestParam("balance") Integer balance) {
        return new ClientDTO(gameClientService.addClient(nickName, email, balance));
    }

    @PutMapping("/{id}")
    public ClientDTO updateClient(@PathVariable Long id,
                                  @RequestParam("nickName") String nickName,
                                  @RequestParam("email") String email,
                                  @RequestParam("balance") Integer balance,
                                  @RequestParam("tankId") Long tankId) {
        return new ClientDTO(gameClientService.updateClient(id, nickName, email, balance, tankService.findTank(tankId)));
    }

    @DeleteMapping("/{id}")
    public ClientDTO deleteClient(@PathVariable Long id) {
        return new ClientDTO(gameClientService.deleteClient(id));
    }
}
