/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.iggy.exception;

import java.util.EnumSet;
import java.util.Optional;
import java.util.Set;

/**
 * Exception thrown when a requested resource is not found on the server.
 *
 * <p>This corresponds to 404-type errors, such as stream not found, topic not found,
 * partition not found, consumer group not found, etc.
 */
public class IggyResourceNotFoundException extends IggyServerException {

    private static final Set<IggyErrorCode> CODES = EnumSet.of(
            IggyErrorCode.RESOURCE_NOT_FOUND,
            IggyErrorCode.CANNOT_LOAD_RESOURCE,
            IggyErrorCode.STREAM_ID_NOT_FOUND,
            IggyErrorCode.STREAM_NAME_NOT_FOUND,
            IggyErrorCode.TOPIC_ID_NOT_FOUND,
            IggyErrorCode.TOPIC_NAME_NOT_FOUND,
            IggyErrorCode.PARTITION_NOT_FOUND,
            IggyErrorCode.SEGMENT_NOT_FOUND,
            IggyErrorCode.CLIENT_NOT_FOUND,
            IggyErrorCode.CONSUMER_GROUP_ID_NOT_FOUND,
            IggyErrorCode.CONSUMER_GROUP_NAME_NOT_FOUND,
            IggyErrorCode.CONSUMER_GROUP_NOT_JOINED,
            IggyErrorCode.MESSAGE_NOT_FOUND);

    /**
     * Constructs a new IggyResourceNotFoundException.
     *
     * @param errorCode the error code enum
     * @param rawErrorCode the raw numeric error code from the server
     * @param reason the error reason/message
     * @param field the optional field related to the error
     * @param errorId the optional error ID for correlation with server logs
     */
    public IggyResourceNotFoundException(
            IggyErrorCode errorCode,
            int rawErrorCode,
            String reason,
            Optional<String> field,
            Optional<String> errorId) {
        super(errorCode, rawErrorCode, reason, field, errorId);
    }

    /**
     * Returns whether the given error code should map to this exception type.
     *
     * @param code the error code to check
     * @return true if this exception type handles the given error code
     */
    public static boolean matches(IggyErrorCode code) {
        return CODES.contains(code);
    }
}
