// Patched from https://raw.githubusercontent.com/etkecc/baibot/v1.13.0/src/entity/globalconfig/entity.rs
// Implements BAIBOT_SEND_N_NEWEST_MESSAGES=N (e.g., =1) for Letta integration.
// Letta manages its own conversation memory, so we only need to send the last N messages.

use mxlink::matrix_sdk::ruma::events::macros::EventContent;

use serde::{Deserialize, Serialize};

use mxlink::helpers::account_data_config::GlobalConfig as GlobalConfigTrait;
use mxlink::helpers::account_data_config::GlobalConfigCarrierContent as GlobalConfigCarrierContentTrait;

use crate::agent::AgentDefinition;
use crate::entity::roomconfig::RoomSettings;

#[derive(Clone, Debug, Default, Deserialize, Serialize, EventContent)]
#[ruma_event(type = "cc.etke.baibot.global_config", kind = GlobalAccountData)]
pub struct GlobalConfigCarrierContent {
    pub payload: String,
}

impl GlobalConfigCarrierContentTrait for GlobalConfigCarrierContent {
    fn payload(&self) -> &str {
        &self.payload
    }

    fn new(payload: String) -> Self {
        Self { payload }
    }
}

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct GlobalConfig {
    pub fallback_room_settings: RoomSettings,

    pub access: GlobalConfigAccess,

    pub agents: Vec<AgentDefinition>,

    // FlowKraft Patch: When set to a positive integer, limits the number of
    // messages sent to the LLM for context. Only the N most recent messages
    // in a thread/reply chain will be included.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub send_n_newest_messages: Option<i64>,
}

impl GlobalConfig {
    pub fn new(user_patterns: Option<Vec<String>>, send_n_newest_messages: Option<i64>) -> Self {
        Self {
            fallback_room_settings: RoomSettings::default(),

            access: GlobalConfigAccess {
                user_patterns,
                room_local_agent_manager_patterns: None,
            },

            agents: vec![],

            // FlowKraft Patch
            send_n_newest_messages,
        }
    }
}

impl GlobalConfigTrait for GlobalConfig {}

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct GlobalConfigAccess {
    // Contains a list of patterns that will be used to specify the "allowed bot users".
    // These remain as patterns and are turned into regex and made use of on demand.
    // Example: `["@*:example.com"]`
    pub user_patterns: Option<Vec<String>>,

    // Contains a list of patterns that will be used to specify "allowed room-local agent managers".
    // These remain as patterns and are turned into regex and made use of on demand.
    // Example: `["@*:example.com"]`
    pub room_local_agent_manager_patterns: Option<Vec<String>>,
}
