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

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

import cz.muni.fi.pa165.soccermanager.dto.LeagueDto;
import cz.muni.fi.pa165.soccermanager.dto.request.CreateLeagueRequest;
import cz.muni.fi.pa165.soccermanager.service.LeagueService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import java.util.Optional;

import io.swagger.v3.oas.annotations.tags.Tag;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@Tag(name = "League", description = "Superior League which is played in soccer manager")
@RestController
@RequestMapping(API_URL)
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
@SecurityRequirement(name = "bearerAuth")
public class LeagueController {

    private final LeagueService leagueService;

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

    @Operation(summary = "Create league",
        description = "The purpose of this method is to create league. Available only to Admin role.")
    @PostMapping("/leagues")
    public ResponseEntity<LeagueDto> createLeague(@Valid @RequestBody CreateLeagueRequest request) {
        final var next = leagueService.createLeague(request);
        final var l = new LeagueDto();
        l.setId(next);
        return ResponseEntity.ok(l);
    }

    @Operation(summary = "Get league by id",
        description = "The purpose of this method is to provide league. Available to Admin and User roles.")
    @GetMapping("/leagues/{leagueId}")
    public ResponseEntity<LeagueDto> getLeagueById(@PathVariable Long leagueId) {
        return ResponseEntity.of(leagueService.findLeagueById(leagueId));
    }

    @Operation(summary = "Update league",
        description = "The purpose of this method is to update league. Available only to Admin role.")
    @PutMapping("/leagues/{leagueId}")
    public ResponseEntity<Void> updateLeague(@Valid @RequestBody LeagueDto newLeagueDto, @PathVariable Long leagueId) {
        leagueService.update(leagueId,newLeagueDto);
        return ResponseEntity.accepted().build();
    }

    @Operation(summary = "Delete league",
        description = "The purpose of this method is to delete league. Available only to Admin role.")
    @DeleteMapping("/leagues/{leagueId}")
    public ResponseEntity<Void> remove(@PathVariable Long leagueId){
        leagueService.remove(leagueId);
        return ResponseEntity.noContent().build();
    }

    @Operation(summary = "Reset league",
        description = "The purpose of this method is to reset league. Available only to Admin role.")
    @GetMapping("/leagues/{leagueId}/reset")
    public ResponseEntity<Void> resetLeague(@PathVariable Long leagueId) {
        leagueService.resetLeague(leagueId);
        return ResponseEntity.accepted().build();
    }
}