/***
 * Copyright (c) 2025 Rafael Fernández López <ereslibre@curried.software>
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 ***/

//! [Business webhooks API v2](https://developer.revolut.com/docs/business/webhooks-v-2).

use crate::{
    business::ApiResult,
    business::client::{BusinessAuthentication, Environment, HttpMethod},
    client::{Body, Client},
};

use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct WebhookRequest {
    pub url: String,
    pub events: Option<Vec<WebhookEvent>>,
}

#[derive(Clone, Debug, Deserialize, Serialize, strum::Display, strum::EnumString)]
#[serde(rename_all = "PascalCase")]
pub enum WebhookEvent {
    TransactionCreated,
    TransactionStateChanged,
    PayoutLinkCreated,
    PayoutLinkStateChanged,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Webhook {
    pub id: String,
    pub url: String,
    pub events: Vec<WebhookEvent>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct WebhookWithSigningSecret {
    pub id: String,
    pub url: String,
    pub events: Vec<WebhookEvent>,
    pub signing_secret: String,
}

#[derive(Debug, Default, Deserialize, Serialize)]
pub struct RotateWebhookSigningSecretRequest {
    pub expiration_period: Option<String>,
}

#[derive(Debug, Default, Deserialize, Serialize)]
pub struct ListParams {
    pub limit: Option<u16>,
    pub created_before: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct FailedWebhookEvent {
    pub id: String,
    pub created_at: String,
    pub updated_at: String,
    pub webhook_id: String,
    pub webhook_url: String,
    pub payload: String,
    pub last_sent_date: Option<String>,
}

impl std::fmt::Display for ListParams {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let query = [
            ("limit", &self.limit.map(|limit| limit.to_string())),
            ("created_before", &self.created_before),
        ]
        .iter()
        .fold(String::new(), |acc, (key, value)| {
            if let Some(value) = value {
                let value = urlencoding::encode(value);
                if acc.is_empty() {
                    format!("{acc}?{key}={value}")
                } else {
                    format!("{acc}&{key}={value}")
                }
            } else {
                acc
            }
        });
        write!(f, "{query}")
    }
}

pub async fn create<E: Environment>(
    client: &Client<E, BusinessAuthentication>,
    webhook: &WebhookRequest,
) -> ApiResult<WebhookWithSigningSecret> {
    client
        .request(
            HttpMethod::Post {
                body: Some(Body::Json(&webhook)),
            },
            &client.environment.uri("2.0", "/webhooks"),
        )
        .await
}

pub async fn list<E: Environment>(
    client: &Client<E, BusinessAuthentication>,
) -> ApiResult<Vec<Webhook>> {
    client
        .request(
            HttpMethod::Get::<()>,
            &client.environment.uri("2.0", "/webhooks"),
        )
        .await
}

pub async fn retrieve<E: Environment>(
    client: &Client<E, BusinessAuthentication>,
    webhook_id: &str,
) -> ApiResult<Webhook> {
    client
        .request(
            HttpMethod::Get::<()>,
            &client
                .environment
                .uri("2.0", &format!("/webhooks/{webhook_id}")),
        )
        .await
}

pub async fn update<E: Environment>(
    client: &Client<E, BusinessAuthentication>,
    webhook_id: &str,
    webhook: &WebhookRequest,
) -> ApiResult<Webhook> {
    client
        .request(
            HttpMethod::Patch {
                body: Some(Body::Json(&webhook)),
            },
            &client
                .environment
                .uri("2.0", &format!("/webhooks/{webhook_id}")),
        )
        .await
}

pub async fn delete<E: Environment>(
    client: &Client<E, BusinessAuthentication>,
    webhook_id: &str,
) -> ApiResult<()> {
    client
        .request(
            HttpMethod::Delete::<()>,
            &client
                .environment
                .uri("2.0", &format!("/webhooks/{webhook_id}")),
        )
        .await
}

pub async fn rotate_signing_secret<E: Environment>(
    client: &Client<E, BusinessAuthentication>,
    webhook_id: &str,
    rotate_webhook_signing_secret: &RotateWebhookSigningSecretRequest,
) -> ApiResult<WebhookWithSigningSecret> {
    client
        .request(
            HttpMethod::Post {
                body: Some(Body::Json(&rotate_webhook_signing_secret)),
            },
            &client.environment.uri(
                "1.0",
                &format!("/webhooks/{webhook_id}/rotate-signing-secret"),
            ),
        )
        .await
}

pub async fn failed_webhook_events<E: Environment>(
    client: &Client<E, BusinessAuthentication>,
    webhook_id: &str,
    list_params: &ListParams,
) -> ApiResult<Vec<FailedWebhookEvent>> {
    client
        .request(
            HttpMethod::Get::<()>,
            &client.environment.uri(
                "2.0",
                &format!("/webhooks/{webhook_id}/failed-events{list_params}"),
            ),
        )
        .await
}

#[cfg(test)]
mod tests {
    use super::*;

    impl Default for FailedWebhookEvent {
        fn default() -> Self {
            Self {
                id: "some-failed-webhook-event-id".to_string(),
                created_at: "some-created-at".to_string(),
                updated_at: "some-updated-at".to_string(),
                webhook_id: "some-webhook-id".to_string(),
                webhook_url: "some-webhook-url".to_string(),
                payload: "some-payload".to_string(),
                last_sent_date: None,
            }
        }
    }

    impl Default for WebhookEvent {
        fn default() -> Self {
            Self::PayoutLinkCreated
        }
    }

    impl Default for Webhook {
        fn default() -> Self {
            Self {
                id: "some-webhook-id".to_string(),
                url: "some-webhook-url".to_string(),
                events: Vec::new(),
            }
        }
    }

    impl Default for WebhookWithSigningSecret {
        fn default() -> Self {
            Self {
                id: "some-webhook-signing-secret-id".to_string(),
                url: "some-webhook-url".to_string(),
                events: Vec::new(),
                signing_secret: "some-webhook-signing-secret".to_string(),
            }
        }
    }
}
