// 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 oparl_types::{DateTime, Name};
use pib_service_core_types::{AgendaItemId, BodyId, Entity, MeetingId};
use serde::{Deserialize, Serialize};

use crate::Updatable;

pub mod get;
pub mod post;

pub mod by_id;

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
pub struct AgendaItemParentId {
    pub body_id: BodyId,
    pub meeting_id: MeetingId,
}

impl Entity<AgendaItemParentId, AgendaItemId> for AgendaItem {}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct AgendaItem {
    pub number: Option<String>,
    pub order: u32,
    pub name: Option<Name>,
    pub public: Option<bool>,
    pub result: Option<String>,
    pub resolution_text: Option<String>,
    pub start: Option<DateTime>,
    pub end: Option<DateTime>,
    pub keyword: Option<Vec<String>>,
    pub license: Option<String>,
    pub web: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct UpdateAgendaItem {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub number: Option<Option<String>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub order: Option<u32>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<Option<Name>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub public: Option<Option<bool>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub result: Option<Option<String>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub resolution_text: Option<Option<String>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub start: Option<Option<DateTime>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub end: Option<Option<DateTime>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub keyword: Option<Option<Vec<String>>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub license: Option<Option<String>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub web: Option<Option<String>>,
}

impl Updatable for AgendaItem {
    type Update = UpdateAgendaItem;

    fn build_update_from(
        &self,
        Self {
            number,
            order,
            name,
            public,
            result,
            resolution_text,
            start,
            end,
            keyword,
            license,
            web,
        }: &Self,
    ) -> Self::Update {
        let number = self.number.build_update_from(number);
        let order = self.order.build_update_from(order);
        let name = self.name.build_update_from(name);
        let public = self.public.build_update_from(public);
        let result = self.result.build_update_from(result);
        let resolution_text = self.resolution_text.build_update_from(resolution_text);
        let start = self.start.build_update_from(start);
        let end = self.end.build_update_from(end);
        let keyword = self.keyword.build_update_from(keyword);
        let license = self.license.build_update_from(license);
        let web = self.web.build_update_from(web);

        Self::Update {
            number,
            order,
            name,
            public,
            result,
            resolution_text,
            start,
            end,
            keyword,
            license,
            web,
        }
    }
}
