use crate::{
    common::{
        types::{APIError, RequestCallbacks},
        utils::parse_response_from_text,
    },
    types::order::{GetOrderResp, OrderQueryResp, PatchOrderRequest},
};
use reqwest::header::{HeaderMap, HeaderValue, CONTENT_TYPE};

pub async fn patch_order(
    shop_url: &String,
    version: &String,
    access_token: &String,
    callbacks: &RequestCallbacks,
    order_id: &String,
    patch_request: &PatchOrderRequest,
) -> Result<GetOrderResp, APIError> {
    let endpoint = format!(
        "{}/admin/api/{}/orders/{}.json",
        shop_url, version, order_id
    );

    let body_str = serde_json::to_string(&patch_request).unwrap_or_default();

    let mut callback_headers = HeaderMap::new();
    callback_headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));

    callbacks.call_before(&endpoint, Some(&body_str), &callback_headers);

    let client = reqwest::Client::new();
    let response = client
        .put(&endpoint)
        .header("X-Shopify-Access-Token", access_token)
        .header("Content-Type", "application/json")
        .json(&patch_request)
        .send()
        .await;

    match response {
        Ok(resp) => {
            let response_headers = resp.headers().clone();
            let response_text = match resp.text().await {
                Ok(text) => text,
                Err(e) => {
                    let error_msg = format!("<failed to read response body: {}>", e);
                    callbacks.call_after(&endpoint, &error_msg, &response_headers);
                    return Err(APIError::FailedToParse);
                }
            };

            callbacks.call_after(&endpoint, &response_text, &response_headers);

            parse_response_from_text::<GetOrderResp>(&response_text)
        }
        Err(e) => {
            let error_msg = format!("<network error: {}>", e);
            callbacks.call_after(&endpoint, &error_msg, &HeaderMap::new());
            Err(APIError::NetworkError)
        }
    }
}

pub async fn get_order_with_name(
    shop_url: &String,
    version: &String,
    order_name: &String,
    access_token: &String,
    callbacks: &RequestCallbacks,
) -> Result<OrderQueryResp, APIError> {
    let endpoint = format!(
        "{}/admin/api/{}/orders.json?query=name:%23{}&status=any",
        shop_url, version, order_name
    );

    let callback_headers = HeaderMap::new();

    callbacks.call_before(&endpoint, None, &callback_headers);

    let client = reqwest::Client::new();
    let response = client
        .get(&endpoint)
        .header("X-Shopify-Access-Token", access_token)
        .send()
        .await;

    match response {
        Ok(resp) => {
            let response_headers = resp.headers().clone();
            let response_text = match resp.text().await {
                Ok(text) => text,
                Err(e) => {
                    let error_msg = format!("<failed to read response body: {}>", e);
                    callbacks.call_after(&endpoint, &error_msg, &response_headers);
                    return Err(APIError::FailedToParse);
                }
            };

            callbacks.call_after(&endpoint, &response_text, &response_headers);

            parse_response_from_text::<OrderQueryResp>(&response_text)
        }
        Err(e) => {
            let error_msg = format!("<network error: {}>", e);
            callbacks.call_after(&endpoint, &error_msg, &HeaderMap::new());
            Err(APIError::NetworkError)
        }
    }
}

pub async fn get_order_with_id(
    shop_url: &String,
    version: &String,
    access_token: &String,
    callbacks: &RequestCallbacks,
    order_id: &String,
) -> Result<GetOrderResp, APIError> {
    let endpoint = format!(
        "{}/admin/api/{}/orders/{}.json",
        shop_url, version, order_id
    );

    let callback_headers = HeaderMap::new();

    callbacks.call_before(&endpoint, None, &callback_headers);

    let client = reqwest::Client::new();
    let response = client
        .get(&endpoint)
        .header("X-Shopify-Access-Token", access_token)
        .send()
        .await;

    match response {
        Ok(resp) => {
            let response_headers = resp.headers().clone();
            let response_text = match resp.text().await {
                Ok(text) => text,
                Err(e) => {
                    let error_msg = format!("<failed to read response body: {}>", e);
                    callbacks.call_after(&endpoint, &error_msg, &response_headers);
                    return Err(APIError::FailedToParse);
                }
            };

            callbacks.call_after(&endpoint, &response_text, &response_headers);

            parse_response_from_text::<GetOrderResp>(&response_text)
        }
        Err(e) => {
            let error_msg = format!("<network error: {}>", e);
            callbacks.call_after(&endpoint, &error_msg, &HeaderMap::new());
            Err(APIError::NetworkError)
        }
    }
}
