use std::hash::{DefaultHasher, Hash, Hasher};

use build_info::{BuildInfo, VersionControl};
use letterbox_notmuch::SearchSummary;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
pub struct SearchResult {
    pub summary: SearchSummary,
    pub query: String,
    pub page: usize,
    pub results_per_page: usize,
    pub total: usize,
}

#[derive(Serialize, Deserialize, Debug, strum_macros::Display)]
pub enum WebsocketMessage {
    RefreshMessages,
}

pub mod urls {
    pub const MOUNT_POINT: &'static str = "/api";
    pub fn cid_prefix(host: Option<&str>, cid: &str) -> String {
        if let Some(host) = host {
            format!("//{host}/api/cid/{cid}/")
        } else {
            format!("/api/cid/{cid}/")
        }
    }
    pub fn download_attachment(host: Option<&str>, id: &str, idx: &str, filename: &str) -> String {
        if let Some(host) = host {
            format!(
                "//{host}/api/download/attachment/{}/{}/{}",
                id, idx, filename
            )
        } else {
            format!("/api/download/attachment/{}/{}/{}", id, idx, filename)
        }
    }
}
pub fn build_version(bi: fn() -> &'static BuildInfo) -> String {
    fn commit(git: &Option<VersionControl>) -> String {
        let Some(VersionControl::Git(git)) = git else {
            return String::new();
        };
        let mut s = vec!["-".to_string(), git.commit_short_id.clone()];
        if let Some(branch) = &git.branch {
            s.push(format!(" ({branch})"));
        }
        s.join("")
    }
    let bi = bi();

    format!("v{}{}", bi.crate_info.version, commit(&bi.version_control)).to_string()
}
pub fn compute_color(data: &str) -> String {
    let mut hasher = DefaultHasher::new();
    data.hash(&mut hasher);
    format!("#{:06x}", hasher.finish() % (1 << 24))
}
