// src/pages/register/suggestions.rs

use anyhow::Result;
use async_trait::async_trait;
use canvas::{SuggestionItem, SuggestionsProvider};

// Keep the async provider if you want, but add this sync helper and shared data.
const ROLES: &[&str] = &["admin", "moderator", "accountant", "viewer"];

pub fn role_suggestions_sync(query: &str) -> Vec<SuggestionItem> {
    let q = query.to_lowercase();
    ROLES
        .iter()
        .filter(|r| q.is_empty() || r.to_lowercase().contains(&q))
        .map(|r| SuggestionItem {
            display_text: (*r).to_string(),
            value_to_store: (*r).to_string(),
        })
        .collect()
}

pub struct RoleSuggestionsProvider;

#[async_trait]
impl SuggestionsProvider for RoleSuggestionsProvider {
    async fn fetch_suggestions(
        &mut self,
        field_index: usize,
        query: &str,
    ) -> Result<Vec<SuggestionItem>> {
        if field_index != 4 {
            return Ok(Vec::new());
        }
        Ok(role_suggestions_sync(query))
    }
}
