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

use crate::{Backend, Result, traits::ForInventory};

impl Backend {
    pub(crate) async fn body_by_id_meeting_by_id_patch(
        &self,
        user: UserInfo,
        body_id: BodyId,
        meeting_id: MeetingId,
        RequestBody(UpdateMeeting {
            name,
            meeting_state,
            start,
            end,
            keyword,
            license,
            web,
        }): 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_meeting(pib_service_inventory::UpdateMeeting {
                id: meeting_id,
                body_id,
                name,
                start,
                end,
                state: meeting_state.map(ForInventory::for_inventory),
                keyword,
                license,
                web,
            })
            .await?;

        Ok(())
    }
}
