// 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 std::sync::Arc;

use axum::{Extension, Json, extract::State};
use pib_service_api_auth::user::UserInfo;
use pib_service_api_types::me::profile::get::ResponseBody;
use pib_service_facade::Service;

use crate::Result;

pub(crate) async fn handle<E: Into<pib_service_facade::Error> + Send + Sync>(
    State(service): State<Arc<dyn Service<E>>>,
    Extension(user): Extension<UserInfo>,
) -> Result<Json<ResponseBody>>
where
    crate::Error: From<E>,
{
    Ok(Json(service.handle_profile_me_get(user).await?))
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use crate::Error;

    use super::{ResponseBody, handle};
    use axum::{Extension, extract::State};
    use mockall::predicate::eq;
    use pib_service_api_auth::user::{Issuer, OidcSub, UserInfo};
    use pib_service_core_types::UserId;
    use pib_service_facade::MockService;
    use pretty_assertions::{assert_eq, assert_matches};

    #[tokio::test]
    async fn valid() {
        let mut service = MockService::<pib_service_facade::Error>::new();

        let user_info = UserInfo {
            id: UserId::from_u128(0x5555),
            issuer: Issuer(String::from("http://auth.example.com")),
            sub: OidcSub("max.mustermann".to_string()),
            display_name: Some("Max Mustermann".to_string()),
            is_superuser: false,
        };

        service
            .expect_handle_profile_me_get()
            .times(1)
            .with(eq(user_info))
            .return_once(|_| {
                Ok(ResponseBody {
                    display_name: Some("hello".to_string()),
                })
            });

        assert_eq!(
            handle(
                State(Arc::new(service)),
                Extension(UserInfo {
                    id: UserId::from_u128(0x5555),
                    issuer: Issuer("http://auth.example.com".to_string()),
                    sub: OidcSub("max.mustermann".to_string()),
                    display_name: Some("Max Mustermann".to_string()),
                    is_superuser: false,
                })
            )
            .await
            .unwrap()
            .0,
            ResponseBody {
                display_name: Some("hello".to_string())
            }
        );
    }

    #[tokio::test]
    async fn invalid() {
        let mut service = MockService::new();

        let user_info = UserInfo {
            id: UserId::from_u128(0x5555),
            issuer: Issuer(String::from("http://auth.example.com")),
            sub: OidcSub("max.mustermann".to_string()),
            display_name: Some("Max Mustermann".to_string()),
            is_superuser: false,
        };

        service
            .expect_handle_profile_me_get()
            .times(1)
            .with(eq(user_info.clone()))
            .return_once(|_| Err(pib_service_facade::Error::NotFound));

        assert_matches!(
            handle(State(Arc::new(service)), Extension(user_info.clone())).await,
            Err(Error::Service {
                source: pib_service_facade::Error::NotFound
            })
        );
    }
}
