//! Admin management of the built-in delivery options (Packeta, DPD).
//!
//! The options themselves are fixed and seeded by `initializers::shipping_seeder`
//! — they cannot be added or removed here. The admin only sets each one's price
//! and toggles whether it is offered at checkout.

use axum_extra::extract::cookie::CookieJar;
use loco_rs::prelude::*;
use sea_orm::{ActiveModelTrait, EntityTrait, QueryOrder, Set};
use serde::Deserialize;
use serde_json::json;

use crate::{
    models::shipping_methods,
    controllers::i18n::current_lang,
    shared::{
        guard,
        money::{format_price, parse_price_to_cents},
        shipping as shipping_rules,
    },
};

#[derive(Debug, Deserialize)]
struct ShippingForm {
    price: String,
    enabled: Option<String>,
}

fn is_checked(value: &Option<String>) -> bool {
    matches!(value.as_deref(), Some("on" | "true" | "1"))
}

#[debug_handler]
async fn index(
    auth: auth::JWT,
    jar: CookieJar,
    ViewEngine(v): ViewEngine<TeraView>,
    State(ctx): State<AppContext>,
) -> Result<Response> {
    guard::current_admin(auth, &ctx).await?;
    shipping_rules::disable_packeta_if_unconfigured(&ctx).await?;
    let methods = shipping_methods::Entity::find()
        .order_by_asc(shipping_methods::Column::Position)
        .all(&ctx.db)
        .await?;
    let packeta_ready = shipping_rules::packeta_ready(&ctx);
    let rows: Vec<serde_json::Value> = methods
        .iter()
        .map(|m| {
            let packeta_not_ready = m.carrier == "packeta" && !packeta_ready;
            let locked = packeta_not_ready && !m.enabled;
            json!({
                "id": m.id,
                "code": m.code,
                "name": m.name,
                "price": format_price(m.price_cents),
                "carrier": m.carrier,
                "requires_pickup_point": m.requires_pickup_point,
                "enabled": m.enabled,
                "packeta_not_ready": packeta_not_ready,
                "locked": locked,
                "lock_reason": if packeta_not_ready { Some("shipping-packeta-missing-settings") } else { None::<&str> },
            })
        })
        .collect();
    format::view(
        &v,
        "admin/shipping/index.html",
        json!({ "methods": rows, "lang": current_lang(&jar) }),
    )
}

#[debug_handler]
async fn update(
    auth: auth::JWT,
    Path(id): Path<i32>,
    State(ctx): State<AppContext>,
    Form(form): Form<ShippingForm>,
) -> Result<Response> {
    guard::current_admin(auth, &ctx).await?;
    let method = shipping_methods::Entity::find_by_id(id)
        .one(&ctx.db)
        .await?
        .ok_or_else(|| Error::NotFound)?;
    let requested_enabled = is_checked(&form.enabled);
    if requested_enabled && method.carrier == "packeta" && !shipping_rules::packeta_ready(&ctx) {
        return Err(Error::BadRequest(
            "Packeta cannot be enabled until PACKETA_API_KEY, PACKETA_API_PASSWORD and PACKETA_SENDER_LABEL are configured.".to_string(),
        ));
    }
    let mut active = method.into_active_model();
    active.price_cents = Set(parse_price_to_cents(&form.price)?);
    active.enabled = Set(requested_enabled);
    active.update(&ctx.db).await?;
    format::redirect("/admin/shipping")
}

pub fn routes() -> Routes {
    Routes::new()
        .add("/admin/shipping", get(index))
        .add("/admin/shipping/{id}", post(update))
}
