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

import io.gmss.fiscad.exceptions.*;
import io.gmss.fiscad.interfaces.infocad.metier.ActeurConcerneService;
import io.gmss.fiscad.interfaces.infocad.metier.PersonneService;
import io.gmss.fiscad.paylaods.ApiResponse;
import io.gmss.fiscad.paylaods.dto.PersonneCompletDTO;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.client.HttpClientErrorException;

@RestController
@RequestMapping(value = "api/acteur-concerne", produces = MediaType.APPLICATION_JSON_VALUE)
@SecurityRequirement(name = "bearer")
@Tag(name = "Actuer concerné")
@CrossOrigin(origins = "*")
@PreAuthorize("hasRole('ADMIN') or hasRole('SUPERVISEUR') or hasRole('ENQUETEUR')")
public class ActeurConcerneController {

    private final ActeurConcerneService acteurConcerneService;
    private final PersonneService personneService;

    private static final Logger logger = LoggerFactory.getLogger(ActeurConcerneController.class);

    public ActeurConcerneController(ActeurConcerneService acteurConcerneService, PersonneService personneService) {
        this.acteurConcerneService = acteurConcerneService;
        this.personneService = personneService;
    }


    @GetMapping("/id/{id}")
    public ResponseEntity<?> getArrondissementById(@PathVariable Long id) {
        try {
            return new ResponseEntity<>(
                    new ApiResponse<>(true, acteurConcerneService.getActeurConcerneById(id).get(), "Arrondissement trouvé avec succès."),
                    HttpStatus.OK
            );
        } catch (HttpClientErrorException.MethodNotAllowed e) {
            logger.error(e.getLocalizedMessage());
            return new ResponseEntity<>(new ApiResponse(false, null, "Method POST/GET is required."), HttpStatus.OK);
        } catch (NotFoundException | BadRequestException | MyFileNotFoundException | ResourceNotFoundException |
                 FileStorageException e) {
            logger.error(e.getLocalizedMessage());
            return new ResponseEntity<>(new ApiResponse(false, null, e.getMessage()), HttpStatus.OK);
        } catch (NullPointerException e) {
            logger.error(e.getLocalizedMessage());
            return new ResponseEntity<>(new ApiResponse(false, null, "Null value has been detected {" + e.getMessage() + "}."), HttpStatus.OK);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return new ResponseEntity<>(new ApiResponse(false, null, "An error has been occur and the content is {" + e.getMessage() + "}."), HttpStatus.OK);
        }
    }
    @GetMapping("/get-details/{id}")
    public ResponseEntity<?> getDetails(@PathVariable Long id)   {
        try{
            PersonneCompletDTO personneCompletDTO = personneService.getPersonneComplete(id);
            return new ResponseEntity<>(
                    new ApiResponse<>(true, personneCompletDTO, "Personne retrouvée avec  succès."),
                    HttpStatus.OK
            );
        }catch (Exception e){
            return new ResponseEntity<>(
                    new ApiResponse<>(false, e.getMessage()),
                    HttpStatus.OK
            );
        }
    }
}

