// 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 pib_service_api_auth::user::UserInfo;
use pib_service_api_permissions::{Method, Resource};
use pib_service_api_types::body::by_id::person::{UpdatePerson, by_id::patch::RequestBody};
use pib_service_core_types::{BodyId, PersonId};

use crate::{Backend, Result};

impl Backend {
    pub(crate) async fn body_by_id_person_by_id_patch(
        &self,
        user: UserInfo,
        body_id: BodyId,
        person_id: PersonId,
        RequestBody(UpdatePerson {
            name,
            family_name,
            given_name,
            form_of_address,
            affix,
            title,
            gender,
            phone,
            email,
        }): RequestBody,
    ) -> Result<()> {
        self.enforce_permissions(Resource::BodyById { body_id }, Method::PATCH, user.id)
            .await?;

        let mut inventory = self.inventory_provider.get_inventory().await?;

        inventory
            .update_person(pib_service_inventory::UpdatePerson {
                person_id,
                body_id,
                name,
                family_name,
                given_name,
                form_of_address,
                affix,
                title,
                gender,
                phone,
                email,
            })
            .await?;

        Ok(())
    }
}
