// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed 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.

use nv_redfish_core::Bmc;
use serde_json::Error as JsonError;
use std::error::Error as StdError;
use std::fmt::Debug;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;

/// Redfish Errors.
pub enum Error<B: Bmc> {
    /// Errors generated by BMC access.
    Bmc(B::Error),
    /// No available account slot found. This error happens for
    /// `slot_defined_user_accounts` feature.
    #[cfg(feature = "accounts")]
    AccountSlotNotAvailable,
    /// Action not available for this resource
    ActionNotAvailable,
    /// Event service does not provide `ServerSentEventUri`
    #[cfg(feature = "event-service")]
    EventServiceServerSentEventUriNotAvailable,
    /// Update service does not provide `MultipartHttpPushUri`
    #[cfg(feature = "update-service")]
    UpdateServiceMultipartHttpPushUriNotAvailable,
    /// Metric definitions are not available for telemetry service
    #[cfg(feature = "telemetry-service")]
    MetricDefinitionsNotAvailable,
    /// Metric report definitions are not available for telemetry service
    #[cfg(feature = "telemetry-service")]
    MetricReportDefinitionsNotAvailable,
    /// JSON parse error.
    Json(JsonError),
}

impl<B: Bmc> Display for Error<B> {
    #[allow(clippy::too_many_lines)]
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        match self {
            Self::Bmc(err) => write!(f, "BMC error: {err}"),
            Self::Json(err) => write!(f, "JSON error: {err}"),
            #[cfg(feature = "accounts")]
            Self::AccountSlotNotAvailable => {
                write!(f, "Free account slot is not found")
            }
            Self::ActionNotAvailable => {
                write!(f, "Action is not available for this resource")
            }
            #[cfg(feature = "event-service")]
            Self::EventServiceServerSentEventUriNotAvailable => {
                write!(f, "Event service does not provide ServerSentEventUri")
            }
            #[cfg(feature = "update-service")]
            Self::UpdateServiceMultipartHttpPushUriNotAvailable => {
                write!(f, "Update service does not provide MultipartHttpPushUri")
            }
            #[cfg(feature = "telemetry-service")]
            Self::MetricDefinitionsNotAvailable => {
                write!(f, "Metric definitions are not available")
            }
            #[cfg(feature = "telemetry-service")]
            Self::MetricReportDefinitionsNotAvailable => {
                write!(f, "Metric report definitions are not available")
            }
        }
    }
}

impl<B: Bmc> Debug for Error<B> {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        Display::fmt(self, f)
    }
}

impl<B: Bmc> StdError for Error<B> {}
