//! DPD shipment creation. See `docs/integrations/dpd.md`.
//!
//! DPD's API (REST vs SOAP, base URL, exact field names) depends on your
//! country and contract, so the request body below must be finalised against
//! *your* DPD account before going live. The surrounding workflow — admin
//! trigger, tracking storage, status update — is fully wired; only the HTTP
//! call is left as a clearly-marked TODO so we don't ship an unverified payload
//! that silently produces broken shipments.

use loco_rs::prelude::*;

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

pub async fn create_shipment(ctx: &AppContext, _req: ShipmentRequest<'_>) -> Result<ShipmentResult> {
    // Required settings (add to config/*.yaml under `settings:` — see docs).
    let _base = settings::get(ctx, "dpd_api_base").filter(|s| !s.is_empty());
    let _login = settings::get(ctx, "dpd_login").filter(|s| !s.is_empty());
    let _password = settings::get(ctx, "dpd_password").filter(|s| !s.is_empty());
    let _customer = settings::get(ctx, "dpd_customer_number").filter(|s| !s.is_empty());

    if _base.is_none() || _login.is_none() || _password.is_none() || _customer.is_none() {
        return Err(Error::BadRequest(
            "DPD is not configured: set settings.dpd_api_base / dpd_login / dpd_password / dpd_customer_number (see docs/integrations/dpd.md)".to_string(),
        ));
    }

    // TODO(dpd): implement once the contract's API variant is known:
    //   1. POST login (delisId + password) -> auth token (cache it).
    //   2. POST storeOrders with the token: recipient from `_req` (or
    //      parcelShopId = _req.pickup_point_id for DPD Pickup), weight,
    //      cod = _req.cod_cents, reference = _req.order_number.
    //   3. Parse the returned parcel number (tracking) + label, then return:
    //      Ok(ShipmentResult { shipment_id, tracking_number, label_url })
    Err(Error::BadRequest(
        "DPD shipment API not finalised yet — fill in the request in src/integrations/dpd.rs per your DPD contract (docs/integrations/dpd.md)".to_string(),
    ))
}
