package cz.muni.fi.pa165.soccermanager.controllers;


import static cz.muni.fi.pa165.soccermanager.ApplicationConstants.API_URL;

import cz.muni.fi.pa165.soccermanager.dto.ChampionshipDto;
import cz.muni.fi.pa165.soccermanager.dto.request.CreateChampionshipRequest;
import cz.muni.fi.pa165.soccermanager.service.ChampionshipService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.Optional;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "Championship", description = "Championship from real world")
@RestController
@RequestMapping(API_URL)
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
@SecurityRequirement(name = "bearerAuth")
public class ChampionshipController {

    private final ChampionshipService championshipService;

    @Operation(summary = "Get championship by id",
        description = "The purpose of this method is to provide championship. Available to Admin and User roles.")
    @GetMapping("championships/{championshipId}")
    public ResponseEntity<ChampionshipDto> getChampionshipById(@PathVariable Long championshipId) {
        return ResponseEntity.of(championshipService.findChampionshipById(championshipId));
    }

    @Operation(summary = "Update championship",
        description = "The purpose of this method is to update championship. Available only to Admin role.")
    @PutMapping("championships/{championshipId}")
    public ResponseEntity<Void> updateChampionship(@Valid @RequestBody ChampionshipDto newChampDTO, @PathVariable Long championshipId) {
        championshipService.update(championshipId, newChampDTO);
        return ResponseEntity.accepted().build();
    }

    @Operation(summary = "Get all championships",
        description = "The purpose of this method is to provide championships. Available to Admin and User roles.")
    @GetMapping("/championships")
    public Page<ChampionshipDto> getChampionships(@RequestParam("page") Optional<Integer> page,
                                                   @RequestParam("size") Optional<Integer> size, @RequestParam Optional<String> sortBy){
        return championshipService.getAllChampionships(page.orElse(0), size.orElse(10), sortBy.orElse("id"));
    }

    @Operation(summary = "Create championship",
        description = "The purpose of this method is to create championship. Available only to Admin role.")
    @PostMapping("/championships")
    public ResponseEntity<ChampionshipDto> createChampionship(@Valid @RequestBody CreateChampionshipRequest request) {
        final var next = championshipService.createChampionship(request);
        final var c = new ChampionshipDto();
        c.setId(next);
        return ResponseEntity.ok(c);
    }
}
