//! Rust client for [BrewFYI](https://brewfyi.com) REST API.
//!
//! ```rust
//! let client = brewfyi::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://brewfyi.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 brew methods.
    pub fn list_brew_methods(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/brew-methods/")
    }

    /// Get brew method by slug.
    pub fn get_brew_method(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/brew-methods/{}/", slug))
    }
    /// List all countries.
    pub fn list_countries(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/countries/")
    }

    /// Get country by slug.
    pub fn get_country(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/countries/{}/", 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 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 processing methods.
    pub fn list_processing_methods(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/processing-methods/")
    }

    /// Get processing method by slug.
    pub fn get_processing_method(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/processing-methods/{}/", slug))
    }
    /// List all regions.
    pub fn list_regions(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/regions/")
    }

    /// Get region by slug.
    pub fn get_region(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/regions/{}/", slug))
    }
    /// List all roast levels.
    pub fn list_roast_levels(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/roast-levels/")
    }

    /// Get roast level by slug.
    pub fn get_roast_level(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/roast-levels/{}/", slug))
    }
    /// List all species.
    pub fn list_species(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/species/")
    }

    /// Get specy by slug.
    pub fn get_specy(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/species/{}/", slug))
    }
    /// List all tools.
    pub fn list_tools(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/tools/")
    }

    /// Get tool by slug.
    pub fn get_tool(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/tools/{}/", slug))
    }
    /// List all varieties.
    pub fn list_varieties(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/varieties/")
    }

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

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