use maud::html;
use rocket::{get, State};
use serde_json::json;

use crate::library::Library;

use super::vec_to_api;

#[get("/search?<query>&<offset>")]
pub async fn search(
    query: &str,
    offset: Option<i64>,
    library: &State<Library>,
) -> Option<serde_json::Value> {
    const NUM_OF_RESULTS: i64 = 20;

    // get start parameter for search result chunks
    let start = offset.unwrap_or(0);

    let mut video_matches = library.search_video(query, start, NUM_OF_RESULTS).await;

    Some(json!(vec_to_api(&mut video_matches).await))
}

#[get("/d/<dir>")]
pub async fn channel_page(dir: &str, library: &State<Library>) -> Option<serde_json::Value> {
    let mut dir_videos = library.get_directory_videos(dir).await;

    Some(json!(vec_to_api(&mut dir_videos).await))
}

#[get("/")]
pub async fn index_page(library: &State<Library>) -> String {
    unimplemented!()
}