/*
 * Zernio API
 *
 * API reference for Zernio. Authenticate with a Bearer API key. Base URL: https://zernio.com/api
 *
 * The version of the OpenAPI document: 1.0.4
 * Contact: support@zernio.com
 * Generated by: https://openapi-generator.tech
 */

use super::{configuration, ContentType, Error};
use crate::{apis::ResponseContent, models};
use reqwest;
use serde::{de::Error as _, Deserialize, Serialize};

/// struct for typed errors of method [`delete_inbox_review_reply`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum DeleteInboxReviewReplyError {
    Status401(models::InlineObject),
    Status403(),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`list_inbox_reviews`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ListInboxReviewsError {
    Status401(models::InlineObject),
    Status403(),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`reply_to_inbox_review`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ReplyToInboxReviewError {
    Status401(models::InlineObject),
    Status403(),
    UnknownValue(serde_json::Value),
}

/// Delete a reply to a review (Google Business only). Requires accountId in request body.
pub async fn delete_inbox_review_reply(
    configuration: &configuration::Configuration,
    review_id: &str,
    delete_inbox_review_reply_request: models::DeleteInboxReviewReplyRequest,
) -> Result<models::DeleteInboxReviewReply200Response, Error<DeleteInboxReviewReplyError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_path_review_id = review_id;
    let p_body_delete_inbox_review_reply_request = delete_inbox_review_reply_request;

    let uri_str = format!(
        "{}/v1/inbox/reviews/{reviewId}/reply",
        configuration.base_path,
        reviewId = crate::apis::urlencode(p_path_review_id)
    );
    let mut req_builder = configuration
        .client
        .request(reqwest::Method::DELETE, &uri_str);

    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }
    if let Some(ref token) = configuration.bearer_access_token {
        req_builder = req_builder.bearer_auth(token.to_owned());
    };
    req_builder = req_builder.json(&p_body_delete_inbox_review_reply_request);

    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();
    let content_type = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("application/octet-stream");
    let content_type = super::ContentType::from(content_type);

    if !status.is_client_error() && !status.is_server_error() {
        let content = resp.text().await?;
        match content_type {
            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::DeleteInboxReviewReply200Response`"))),
            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::DeleteInboxReviewReply200Response`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<DeleteInboxReviewReplyError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent {
            status,
            content,
            entity,
        }))
    }
}

/// Fetch reviews from all connected Facebook Pages and Google Business accounts. Aggregates data with filtering and sorting options. Supported platforms: Facebook, Google Business.
pub async fn list_inbox_reviews(
    configuration: &configuration::Configuration,
    profile_id: Option<&str>,
    platform: Option<&str>,
    min_rating: Option<i32>,
    max_rating: Option<i32>,
    has_reply: Option<bool>,
    sort_by: Option<&str>,
    sort_order: Option<&str>,
    limit: Option<i32>,
    cursor: Option<&str>,
    account_id: Option<&str>,
) -> Result<models::ListInboxReviews200Response, Error<ListInboxReviewsError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_query_profile_id = profile_id;
    let p_query_platform = platform;
    let p_query_min_rating = min_rating;
    let p_query_max_rating = max_rating;
    let p_query_has_reply = has_reply;
    let p_query_sort_by = sort_by;
    let p_query_sort_order = sort_order;
    let p_query_limit = limit;
    let p_query_cursor = cursor;
    let p_query_account_id = account_id;

    let uri_str = format!("{}/v1/inbox/reviews", configuration.base_path);
    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);

    if let Some(ref param_value) = p_query_profile_id {
        req_builder = req_builder.query(&[("profileId", &param_value.to_string())]);
    }
    if let Some(ref param_value) = p_query_platform {
        req_builder = req_builder.query(&[("platform", &param_value.to_string())]);
    }
    if let Some(ref param_value) = p_query_min_rating {
        req_builder = req_builder.query(&[("minRating", &param_value.to_string())]);
    }
    if let Some(ref param_value) = p_query_max_rating {
        req_builder = req_builder.query(&[("maxRating", &param_value.to_string())]);
    }
    if let Some(ref param_value) = p_query_has_reply {
        req_builder = req_builder.query(&[("hasReply", &param_value.to_string())]);
    }
    if let Some(ref param_value) = p_query_sort_by {
        req_builder = req_builder.query(&[("sortBy", &param_value.to_string())]);
    }
    if let Some(ref param_value) = p_query_sort_order {
        req_builder = req_builder.query(&[("sortOrder", &param_value.to_string())]);
    }
    if let Some(ref param_value) = p_query_limit {
        req_builder = req_builder.query(&[("limit", &param_value.to_string())]);
    }
    if let Some(ref param_value) = p_query_cursor {
        req_builder = req_builder.query(&[("cursor", &param_value.to_string())]);
    }
    if let Some(ref param_value) = p_query_account_id {
        req_builder = req_builder.query(&[("accountId", &param_value.to_string())]);
    }
    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }
    if let Some(ref token) = configuration.bearer_access_token {
        req_builder = req_builder.bearer_auth(token.to_owned());
    };

    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();
    let content_type = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("application/octet-stream");
    let content_type = super::ContentType::from(content_type);

    if !status.is_client_error() && !status.is_server_error() {
        let content = resp.text().await?;
        match content_type {
            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ListInboxReviews200Response`"))),
            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ListInboxReviews200Response`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<ListInboxReviewsError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent {
            status,
            content,
            entity,
        }))
    }
}

/// Post a reply to a review. Requires accountId in request body.
pub async fn reply_to_inbox_review(
    configuration: &configuration::Configuration,
    review_id: &str,
    reply_to_inbox_review_request: models::ReplyToInboxReviewRequest,
) -> Result<models::ReplyToInboxReview200Response, Error<ReplyToInboxReviewError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_path_review_id = review_id;
    let p_body_reply_to_inbox_review_request = reply_to_inbox_review_request;

    let uri_str = format!(
        "{}/v1/inbox/reviews/{reviewId}/reply",
        configuration.base_path,
        reviewId = crate::apis::urlencode(p_path_review_id)
    );
    let mut req_builder = configuration
        .client
        .request(reqwest::Method::POST, &uri_str);

    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }
    if let Some(ref token) = configuration.bearer_access_token {
        req_builder = req_builder.bearer_auth(token.to_owned());
    };
    req_builder = req_builder.json(&p_body_reply_to_inbox_review_request);

    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();
    let content_type = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("application/octet-stream");
    let content_type = super::ContentType::from(content_type);

    if !status.is_client_error() && !status.is_server_error() {
        let content = resp.text().await?;
        match content_type {
            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ReplyToInboxReview200Response`"))),
            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ReplyToInboxReview200Response`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<ReplyToInboxReviewError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent {
            status,
            content,
            entity,
        }))
    }
}
