use crate::components::StyledHeader;
use crate::components::header::HeaderItem;
use crate::components::header::HeaderItems;
use crate::models::WallUid;
use leptos::prelude::*;

#[component]
#[tracing::instrument(skip_all)]
pub fn Settings() -> impl IntoView {
    let header_items = HeaderItems {
        left: vec![HeaderItem {
            text: "← Ascend".to_string(),
            link: Some("/".to_string()),
        }],
        middle: vec![HeaderItem {
            text: "Settings".to_string(),
            link: None,
        }],
        right: vec![],
    };

    view! {
        <div class="min-h-screen min-w-screen bg-neutral-950">
            <StyledHeader items=header_items />

            // {move || view! { <Import wall_uid=wall_uid.get() /> }}
            <div class="container mx-auto mt-2" />
        </div>
    }
}

#[component]
#[tracing::instrument(skip_all)]
fn Import(wall_uid: WallUid) -> impl IntoView {
    let import_from_mini_moonboard = ServerAction::<ImportFromMiniMoonboard>::new();

    let onclick = move |_mouse_event| {
        import_from_mini_moonboard.dispatch(ImportFromMiniMoonboard { wall_uid });
    };

    view! {
        <p>"Import problems from"</p>
        <button on:click=onclick>"Mini Moonboard"</button>
    }
}

#[server]
#[tracing::instrument]
async fn import_from_mini_moonboard(wall_uid: WallUid) -> Result<(), ServerFnError> {
    use crate::server::config::Config;
    use crate::server::db::Database;

    tracing::info!("Importing mini moonboard problems");

    let config = expect_context::<Config>();
    let db = expect_context::<Database>();

    crate::server::operations::import_mini_moonboard_problems(&config, db, wall_uid).await?;

    // TODO: Return information about what was done
    Ok(())
}
