// SPDX-FileCopyrightText: Politik im Blick developers
// SPDX-FileCopyrightText: Wolfgang Silbermayr <wolfgang@silbermayr.at>
//
// SPDX-License-Identifier: AGPL-3.0-or-later OR EUPL-1.2

use snafu::Snafu;

#[derive(Debug, Snafu)]
#[snafu(visibility(pub))]
pub enum Error {
    #[snafu(display("Access to resource forbidden"))]
    Forbidden,

    #[snafu(display("Error accessing data in the inventory"))]
    Inventory {
        source: pib_service_inventory::Error,
    },

    #[snafu(display("Error enforcing permissions"))]
    Permissions {
        source: pib_service_api_permissions::Error,
    },

    #[snafu(display("Unable to parse meeting state"))]
    ParseMeetingState {
        source: pib_service_inventory::ParseMeetingStateError,
    },
}

impl From<Error> for pib_service_facade::Error {
    fn from(value: Error) -> Self {
        match value {
            Error::Forbidden => pib_service_facade::Error::Forbidden,
            Error::Inventory { source } if source.is_not_found() => {
                pib_service_facade::Error::NotFound
            }
            Error::ParseMeetingState { source } => {
                pib_service_facade::Error::bad_request(format!("{source}"))
            }
            e @ Error::Inventory { .. } | e @ Error::Permissions { .. } => {
                pib_service_facade::Error::Backend {
                    source: Box::new(e),
                }
            }
        }
    }
}

impl From<pib_service_inventory::Error> for Error {
    fn from(source: pib_service_inventory::Error) -> Self {
        Self::Inventory { source }
    }
}

impl From<pib_service_api_permissions::Error> for Error {
    fn from(source: pib_service_api_permissions::Error) -> Self {
        Self::Permissions { source }
    }
}

impl From<pib_service_inventory::ParseMeetingStateError> for Error {
    fn from(source: pib_service_inventory::ParseMeetingStateError) -> Self {
        Self::ParseMeetingState { source }
    }
}
