//! DHL shipment creation. See `docs/integrations/dhl.md`.
//!
//! DHL has several APIs (Parcel DE Shipping, eCommerce, MyDHL Express) behind
//! one developer portal; which one applies depends on your contract and the
//! markets you ship to. As with DPD, the workflow is fully wired — only the
//! authenticated HTTP call is left as a marked TODO so we don't ship an
//! unverified payload.

use loco_rs::prelude::*;

use super::{ShipmentRequest, ShipmentResult};
use crate::shared::settings;

pub async fn create_shipment(ctx: &AppContext, _req: ShipmentRequest<'_>) -> Result<ShipmentResult> {
    let _base = settings::get(ctx, "dhl_api_base").filter(|s| !s.is_empty());
    let _key = settings::get(ctx, "dhl_api_key").filter(|s| !s.is_empty());
    let _secret = settings::get(ctx, "dhl_api_secret").filter(|s| !s.is_empty());
    let _account = settings::get(ctx, "dhl_account_number").filter(|s| !s.is_empty());

    if _base.is_none() || _key.is_none() || _secret.is_none() || _account.is_none() {
        return Err(Error::BadRequest(
            "DHL is not configured: set settings.dhl_api_base / dhl_api_key / dhl_api_secret / dhl_account_number (see docs/integrations/dhl.md)".to_string(),
        ));
    }

    // TODO(dhl): implement once the API subscription is known:
    //   1. OAuth2 client-credentials -> Bearer token (cache until expiry).
    //   2. POST the shipment: shipper = your account/EKP, consignee from
    //      `_req`, product code (domestic/express), weight, references; add
    //      customs data for non-EU destinations.
    //   3. Parse tracking number + label, return ShipmentResult.
    Err(Error::BadRequest(
        "DHL shipment API not finalised yet — fill in the request in src/integrations/dhl.rs per your DHL subscription (docs/integrations/dhl.md)".to_string(),
    ))
}
