package dev.photosync.core.provider;

import lombok.Getter;

/**
 * Anything a backend can fail with, classified well enough for the upload queue
 * to decide between "retry later" and "stop and tell the player".
 */
@Getter
public class ProviderException extends Exception {

    public enum Kind {
        /** Connection refused, DNS failure, timeout. Worth retrying. */
        NETWORK(true),
        /** Bad or revoked credentials. Retrying will not help. */
        AUTHENTICATION(false),
        /** The album or asset is gone. Retrying will not help. */
        NOT_FOUND(false),
        /** Server asked us to slow down. Worth retrying, after a longer wait. */
        RATE_LIMITED(true),
        /** 5xx. The server may recover. */
        SERVER(true),
        /** The response did not look like what the API promised. */
        PROTOCOL(false),
        /** The file we were asked to upload is unreadable or gone. */
        SOURCE_UNREADABLE(false);

        private final boolean retryable;

        Kind(boolean retryable) {
            this.retryable = retryable;
        }

        public boolean isRetryable() {
            return retryable;
        }
    }

    private final Kind kind;

    public ProviderException(Kind kind, String message) {
        super(message);
        this.kind = kind;
    }

    public ProviderException(Kind kind, String message, Throwable cause) {
        super(message, cause);
        this.kind = kind;
    }

    public boolean isRetryable() {
        return kind.isRetryable();
    }
}
