package dev.lions.unionflow.client.service;

import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.Response;
import org.eclipse.microprofile.rest.client.ext.ResponseExceptionMapper;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

public class RestClientExceptionMapper implements ResponseExceptionMapper<RuntimeException> {

    @Override
    public RuntimeException toThrowable(Response response) {
        int status = response.getStatus();
        String reasonPhrase = response.getStatusInfo().getReasonPhrase();
        
        // Lire le corps de la réponse pour plus de détails
        String body = "";
        try {
            if (response.hasEntity()) {
                body = response.readEntity(String.class);
            }
        } catch (Exception e) {
            body = "Impossible de lire le détail de l'erreur";
        }
        
        return switch (status) {
            case 400 -> new BadRequestException("Requête invalide: " + body);
            case 401 -> new UnauthorizedException("Non autorisé: " + reasonPhrase);
            case 403 -> new ForbiddenException("Accès interdit: " + reasonPhrase);
            case 404 -> new NotFoundException("Ressource non trouvée: " + reasonPhrase);
            case 409 -> new ConflictException("Conflit: " + body);
            case 422 -> new UnprocessableEntityException("Données non valides: " + body);
            case 500 -> new InternalServerErrorException("Erreur serveur interne: " + body);
            case 502 -> new BadGatewayException("Erreur de passerelle: " + reasonPhrase);
            case 503 -> new ServiceUnavailableException("Service indisponible: " + reasonPhrase);
            case 504 -> new GatewayTimeoutException("Timeout de passerelle: " + reasonPhrase);
            default -> new UnknownHttpStatusException("Erreur HTTP " + status + ": " + reasonPhrase + (body.isEmpty() ? "" : " - " + body));
        };
    }
    
    // Classes d'exception personnalisées
    public static class BadRequestException extends RuntimeException {
        public BadRequestException(String message) { super(message); }
    }
    
    public static class UnauthorizedException extends RuntimeException {
        public UnauthorizedException(String message) { super(message); }
    }
    
    public static class ForbiddenException extends RuntimeException {
        public ForbiddenException(String message) { super(message); }
    }
    
    public static class NotFoundException extends RuntimeException {
        public NotFoundException(String message) { super(message); }
    }
    
    public static class ConflictException extends RuntimeException {
        public ConflictException(String message) { super(message); }
    }
    
    public static class UnprocessableEntityException extends RuntimeException {
        public UnprocessableEntityException(String message) { super(message); }
    }
    
    public static class InternalServerErrorException extends RuntimeException {
        public InternalServerErrorException(String message) { super(message); }
    }
    
    public static class BadGatewayException extends RuntimeException {
        public BadGatewayException(String message) { super(message); }
    }
    
    public static class ServiceUnavailableException extends RuntimeException {
        public ServiceUnavailableException(String message) { super(message); }
    }
    
    public static class GatewayTimeoutException extends RuntimeException {
        public GatewayTimeoutException(String message) { super(message); }
    }
    
    public static class UnknownHttpStatusException extends RuntimeException {
        public UnknownHttpStatusException(String message) { super(message); }
    }
}