// Copyright (C) 2023-2025 RabbitMQ Core Team (teamrabbitmq@gmail.com)
//
// 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 crate::requests::RuntimeParameterDefinition;
use crate::requests::federation::{FEDERATION_UPSTREAM_COMPONENT, FederationUpstreamParams};
use crate::responses::{FederationLink, FederationUpstream};

use super::client::{Client, Result};
use std::fmt::Display;
use std::result::Result as StdResult;

impl<E, U, P> Client<E, U, P>
where
    E: Display,
    U: Display,
    P: Display,
{
    /// Lists [federation](https://www.rabbitmq.com/docs/federation) upstreams defined in the cluster.
    ///
    /// Requires the `policymaker` user tag. Does not modify state.
    pub async fn list_federation_upstreams(&self) -> Result<Vec<FederationUpstream>> {
        let response = self
            .list_runtime_parameters_of_component(FEDERATION_UPSTREAM_COMPONENT)
            .await?;
        let upstreams = response
            .into_iter()
            .map(FederationUpstream::try_from)
            .collect::<StdResult<Vec<_>, _>>()?;

        Ok(upstreams)
    }

    /// Lists [federation](https://www.rabbitmq.com/docs/federation) links (connections) running in the cluster.
    ///
    /// Requires the `monitoring` user tag. Does not modify state.
    /// Can be used by restricted monitoring users with the `monitoring` tag and only the `read`, `configure` permissions.
    pub async fn list_federation_links(&self) -> Result<Vec<FederationLink>> {
        let response = self.http_get("federation-links", None, None).await?;
        let response = response.json().await?;
        Ok(response)
    }

    /// Creates or updates a [federation](https://www.rabbitmq.com/docs/federation) upstream.
    ///
    /// Federation upstreams define connection endpoints for federation links (connections that federate
    /// queues or exchanges).
    ///
    /// Requires the `policymaker` user tag.
    pub async fn declare_federation_upstream(
        &self,
        params: FederationUpstreamParams<'_>,
    ) -> Result<()> {
        let runtime_param = RuntimeParameterDefinition::from(params);

        self.upsert_runtime_parameter(&runtime_param).await
    }

    /// Gets a specific [federation](https://www.rabbitmq.com/docs/federation) upstream by name and virtual host.
    ///
    /// Requires the `policymaker` user tag. Does not modify state.
    pub async fn get_federation_upstream(
        &self,
        vhost: &str,
        name: &str,
    ) -> Result<FederationUpstream> {
        let param = self
            .get_runtime_parameter(FEDERATION_UPSTREAM_COMPONENT, vhost, name)
            .await?;
        FederationUpstream::try_from(param).map_err(|e| e.into())
    }

    /// Deletes a [federation](https://www.rabbitmq.com/docs/federation) upstream.
    /// Deleting an upstream will stop any links connected to it.
    ///
    /// Requires the `policymaker` user tag.
    pub async fn delete_federation_upstream(
        &self,
        vhost: &str,
        name: &str,
        idempotently: bool,
    ) -> Result<()> {
        self.clear_runtime_parameter(FEDERATION_UPSTREAM_COMPONENT, vhost, name, idempotently)
            .await
    }
}
