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

use crate::{Backend, Result};

impl Backend {
    pub(crate) async fn body_by_id_organization_by_id_patch(
        &self,
        user: UserInfo,
        body_id: BodyId,
        organization_id: OrganizationId,
        RequestBody(UpdateOrganization {
            name,
            short_name,
            classification,
            start_date,
            end_date,
            website,
            color,
        }): 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_organization(pib_service_inventory::UpdateOrganization {
                organization_id,
                body_id,
                name,
                short_name,
                classification,
                start_date,
                end_date,
                website,
                color,
            })
            .await?;

        Ok(())
    }
}
