package org.egl_cepgl.pm.controller.admin;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.egl_cepgl.pm.dto.ApplicantDto;
import org.egl_cepgl.pm.dto.ApplicantQualificationDto;
import org.egl_cepgl.pm.dto.CountryDto;
import org.egl_cepgl.pm.model.Applicant;
import org.egl_cepgl.pm.model.ApplicantQualification;
import org.egl_cepgl.pm.model.Country;
import org.egl_cepgl.pm.service.ApplicantService;
import org.egl_cepgl.pm.service.CountryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Api("api/admin/countries")
@RestController
@RequestMapping("api/admin/countries")
public class CountryController
{
    private CountryService service;

    @Autowired
    public CountryController(CountryService service) {
        this.service = service;
    }

    //@PreAuthorize("hasAuthority('ROLE_default-roles-egl_pm')")
    @PostMapping(value = "/add",consumes= MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value="Add ..", notes="Cette méthode permet de..", response = Country.class)
    @ApiResponses(value= { @ApiResponse(code= 200, message = "créé/modifié"), @ApiResponse(code= 400, message = "Non valide") })
    public ResponseEntity<CountryDto> add(@RequestBody Country obj)
    {
        //log.info("BVDDDDDDDDDDDDEEEE==="+userDto.toString());
        return ResponseEntity.ok(CountryDto.fromEntity(this.service.add(obj)));
    }

    @GetMapping(value = "/all",produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value="List ..", notes="Cette méthode permet..", responseContainer = "List<Country>")
    @ApiResponses(value= { @ApiResponse(code= 200, message = "Une liste des ..") })
    public ResponseEntity<Map<String, Object>> findAll(
            @RequestParam(required = false) String search,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "5") int size
    ){
        Page<Country> objs= service.findAll(search, page, size);
        Map<String, Object> response = new HashMap<>();
        response.put("countries", objs.getContent().stream().map(CountryDto::fromEntity).collect(Collectors.toList()));
        response.put("currentPage", objs.getNumber());
        response.put("totalItems", objs.getTotalElements());
        response.put("totalPages", objs.getTotalPages());

        return new ResponseEntity<>(response, HttpStatus.OK);
    }

    @PutMapping(value = "/update",consumes= MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value="Update ..", notes="Cette méthode permet de..", response = Country.class)
    @ApiResponses(value= { @ApiResponse(code= 200, message = "créé/modifié"), @ApiResponse(code= 400, message = "Non valide") })
    public ResponseEntity<CountryDto> update(@RequestBody Country obj)
    {    return ResponseEntity.ok(CountryDto.fromEntity(this.service.update(obj)));   }

    @DeleteMapping(value = "/delete/{id}")
    @ApiOperation(value = "Suppression d'un..", notes = "Cette méthode permet de supprimer..")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Supprimé")
    })
    ResponseEntity delete(@PathVariable("id") Long id) {
        service.delete(id);
        return ResponseEntity.ok().build();
    }
}
