// Copyright (C) 2025 Huawei Device Co., Ltd.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Notification configuration validation for task configuration.
//!
//! Validates notification title and text lengths, the want agent, and the visibility setting on API10.

use request_core::config::{Action, TaskConfig, Version};

use crate::verify::ConfigVerifier;

/// Verifier for the notification configuration of a task config.
///
/// Validates notification title and text lengths, the want agent, and the
/// visibility setting on API10.
pub struct NotificationVerifier {}

impl ConfigVerifier for NotificationVerifier {
    fn verify(&self, config: &TaskConfig) -> Result<(), i32> {
        const NOTIFICATION_TITLE_MAX_LEN: usize = 1024;
        const NOTIFICATION_TEXT_MAX_LEN: usize = 3072;
        if matches!(config.version, Version::API9) {
            return Ok(());
        }

        if let Some(title) = &config.notification.title {
            if title.len() > NOTIFICATION_TITLE_MAX_LEN {
                error!("notification title length must be less than 1024");
                return Err(401);
            }
        }

        if let Some(text) = &config.notification.text {
            if (text.len() > NOTIFICATION_TEXT_MAX_LEN) {
                error!("notification text length must be less than 3072");
                return Err(401);
            }
        }

        if let Some(ref want_agent) = config.notification.want_agent {
            if want_agent.is_empty() {
                error!("notification want_agent string is empty");
                return Err(401);
            }
        }

        if let Some(visibility) = config.notification.visibility {
            if visibility == 0 || (visibility & 0b11) != visibility {
                error!("notification visibility must be 1 or 2 or 3");
                return Err(401);
            }
        }

        Ok(())
    }
}