package io.gmss.fiscad.controllers.infocad.metier;


import io.gmss.fiscad.interfaces.infocad.metier.ParcelleGeomService;
import io.gmss.fiscad.paylaods.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@RestController
@RequestMapping(value = "api/parcelle-geom", produces = MediaType.APPLICATION_JSON_VALUE)
@SecurityRequirement(name = "bearer")
@Tag(name = "ParcelleGeometrie")
@CrossOrigin(origins = "*")
@PreAuthorize("hasRole('ADMIN') or hasRole('SUPERVISEUR') or hasRole('ENQUETEUR')")
public class ParcelleGeomController {

    private final ParcelleGeomService parcelleGeomService;

    public ParcelleGeomController(ParcelleGeomService parcelleGeomService) {
        this.parcelleGeomService = parcelleGeomService;
    }


    @PostMapping(value = "/create-from-geojsonfile",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity<?> createTpe(@RequestPart(required = true) MultipartFile file,@RequestParam String reference,@RequestParam String description)   {
        try{
            int n = parcelleGeomService.createParcelleFromGeoJsonFile(file, reference, description);
            return new ResponseEntity<>(
                    new ApiResponse<>(true, null, n+" Parcelles crées avec succès"),
                    HttpStatus.OK
            );
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseEntity<>(
                    new ApiResponse<>(false, e.getMessage()),
                    HttpStatus.OK
            );
        }
    }

    @GetMapping("/all")
    public ResponseEntity<?> getAllTpeList() {
        return new ResponseEntity<>(
                new ApiResponse<>(true, parcelleGeomService.getParcelleGeomList(), "Liste des parcelle chargée avec succès."),
                HttpStatus.OK
        );
    }

    @GetMapping("/by-quartier/{codeQuartier}")
    public ResponseEntity<?> getAllTpeList(@PathVariable String codeQuartier) {
        return new ResponseEntity<>(
                new ApiResponse<>(true, parcelleGeomService.getParcelleGeomListUnQuatier(codeQuartier), "Liste des parcelle chargée avec succès."),
                HttpStatus.OK
        );
    }


    @GetMapping("/all-paged")
    public ResponseEntity<?> getAllTpePaged(@RequestParam int pageNo, @RequestParam int pageSize) {
        Pageable pageable = PageRequest.of(pageNo, pageSize);
        return new ResponseEntity<>(
                new ApiResponse<>(true, parcelleGeomService.getParcelleGeomList(pageable), "Liste des parcelle chargée avec succès."),
                HttpStatus.OK
        );
    }

    @GetMapping("/id/{id}")
    public ResponseEntity<?> getParcelleGeomById(@PathVariable Long id) {
        try {
            return new ResponseEntity<>(
                    new ApiResponse<>(true, parcelleGeomService.getParcelleGeomById(id).orElse(null), "parcelle trouvé avec succès."),
                    HttpStatus.OK
            );
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(new ApiResponse(false, null, "An error has been occur and the content is {" + e.getMessage() + "}."), HttpStatus.OK);
        }
    }


}