// 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::{UpdateBody, by_id::patch::RequestBody};
use pib_service_core_types::BodyId;

use crate::{Backend, Result};

impl Backend {
    pub(crate) async fn body_by_id_patch(
        &self,
        user: UserInfo,
        body_id: BodyId,
        RequestBody(UpdateBody {
            short_name,
            name,
            website,
            license,
            license_valid_since,
            oparl_since,
            ags,
            rgs,
            contact_email,
            contact_name,
            classification,
            web,
            is_public,
        }): 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_body(pib_service_inventory::UpdateBody {
                id: body_id,
                short_name,
                name,
                website,
                license,
                license_valid_since,
                oparl_since,
                ags,
                rgs,
                contact_email,
                contact_name,
                classification,
                web,
                is_public,
            })
            .await?;

        Ok(())
    }
}
