//! Rust client for [NameFYI](https://namefyi.com) REST API.
//!
//! ```rust
//! let client = namefyi::Client::new();
//! let result = client.search("query").unwrap();
//! ```

use serde_json::Value;

pub struct Client {
    base_url: String,
    http: reqwest::blocking::Client,
}

impl Client {
    pub fn new() -> Self {
        Self {
            base_url: "https://namefyi.com".to_string(),
            http: reqwest::blocking::Client::new(),
        }
    }

    fn get(&self, path: &str) -> Result<Value, Box<dyn std::error::Error>> {
        let url = format!("{}{}", self.base_url, path);
        let resp = self.http.get(&url).send()?.json()?;
        Ok(resp)
    }

    pub fn search(&self, query: &str) -> Result<Value, Box<dyn std::error::Error>> {
        let url = format!("{}/api/v1/search/?q={}", self.base_url, query);
        let resp = self.http.get(&url).send()?.json()?;
        Ok(resp)
    }

    /// List all characters.
    pub fn list_characters(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/characters/")
    }

    /// Get character by slug.
    pub fn get_character(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/characters/{}/", slug))
    }
    /// List all cultures.
    pub fn list_cultures(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/cultures/")
    }

    /// Get culture by slug.
    pub fn get_culture(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/cultures/{}/", slug))
    }
    /// List all faqs.
    pub fn list_faqs(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/faqs/")
    }

    /// Get faq by slug.
    pub fn get_faq(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/faqs/{}/", slug))
    }
    /// List all given names.
    pub fn list_given_names(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/given-names/")
    }

    /// Get given name by slug.
    pub fn get_given_name(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/given-names/{}/", slug))
    }
    /// List all glossary.
    pub fn list_glossary(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/glossary/")
    }

    /// Get term by slug.
    pub fn get_term(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/glossary/{}/", slug))
    }
    /// List all guides.
    pub fn list_guides(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/guides/")
    }

    /// Get guide by slug.
    pub fn get_guide(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/guides/{}/", slug))
    }
    /// List all meaning tags.
    pub fn list_meaning_tags(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/meaning-tags/")
    }

    /// Get meaning tag by slug.
    pub fn get_meaning_tag(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/meaning-tags/{}/", slug))
    }
    /// List all surnames.
    pub fn list_surnames(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/surnames/")
    }

    /// Get surname by slug.
    pub fn get_surname(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/surnames/{}/", slug))
    }
}

impl Default for Client {
    fn default() -> Self { Self::new() }
}
