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

use crate::{Backend, Result};

impl Backend {
    pub(crate) async fn body_by_id_meeting_by_id_agenda_item_by_id_patch(
        &self,
        user: UserInfo,
        body_id: BodyId,
        meeting_id: MeetingId,
        agenda_item_id: AgendaItemId,
        RequestBody(UpdateAgendaItem {
            number,
            order,
            name,
            public,
            result,
            resolution_text,
            start,
            end,
            keyword,
            license,
            web,
        }): RequestBody,
    ) -> Result<()> {
        self.enforce_permissions(
            Resource::BodyByIdMeetingByIdAgendaItemById {
                body_id,
                meeting_id,
                agenda_item_id,
            },
            Method::PATCH,
            user.id,
        )
        .await?;

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

        inventory
            .update_agenda_item(pib_service_inventory::UpdateAgendaItem {
                id: agenda_item_id,
                body_id,
                meeting_id,
                number,
                order,
                name,
                public,
                result,
                resolution_text,
                start,
                end,
                keyword,
                license,
                web,
            })
            .await?;

        Ok(())
    }
}
