use nostr::ToBech32;
use nostr_sdk::prelude::*;
use std::time::Duration;
use windmark_titanesque::{context::RouteContext, response::Response};

use crate::aska::WindTemplate;
use crate::emoji::*;
use crate::user::CaracalUser;
use askama::Template;
use rust_i18n::t;
use std::borrow::Cow;

#[derive(Template, Clone)]
#[template(path = "nsites_index.gmi", escape = "txt")]
struct NsitesIndexTemplate {
    nsites: Events,
}

pub async fn nsites_index(
    _ctx: RouteContext,
    user: &'static mut CaracalUser,
) -> Response {
    let nsites_filter = Filter::new()
        .kind(Kind::Custom(15128))
        .kind(Kind::Custom(35128));

    let Ok(nsites) = user
        .client
        .fetch_combined_events(nsites_filter, Duration::from_secs(5))
        .await
    else {
        return Response::temporary_failure(t!("fetch_events_failed"));
    };

    Response::success(WindTemplate::render(NsitesIndexTemplate { nsites }))
}
