use ntex::web;

const BASE_URL: &str = "https://jable.tv";

pub async fn redirect_to_page(
    req: web::HttpRequest,
) -> impl web::Responder {
    let slug = req
        .match_info()
        .query("slug")
        .trim_matches('/')
        .to_string();

    if slug.is_empty() {
        return web::HttpResponse::NotFound().finish();
    }

    // HEAD: check.py health check — just confirm the endpoint exists
    if req.method() == ntex::http::Method::HEAD {
        return web::HttpResponse::Ok().finish();
    }

    // GET: open original page in browser
    let location = format!("{BASE_URL}/videos/{slug}/");
    web::HttpResponse::Found()
        .header("Location", location)
        .finish()
}
