use std::path::Path;

mod library;
mod pages;
mod yt_meta;

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    std::env::set_var("RUST_LOG", "info");
    std::env::set_var("RUST_BACKTRACE", "1");
    env_logger::init();

    let args: Vec<String> = std::env::args().collect();

    if args.len() != 2 {
        eprintln!("Usage: {} <directory_path>", args[0]);
        std::process::exit(1);
    }

    let dir_path = args[1].clone();

    let lib = library::Library::new();
    let library = actix_web::web::Data::new(lib);

    let lib = library.clone();

    std::thread::spawn(move || {
        lib.scan_dir(&Path::new(&dir_path.clone()).to_path_buf());
    });

    web_base::map!(
        web_base::Site::new()
            .enable_picocss(true)
            .enable_htmx(true)
            .enable_favicon("/icon".to_owned())
            .add_manifest(
                web_base::Manifest::new("WatchDogs")
                    .set_short_name("WatchDogs")
                    .set_background_color("rgb(1, 4, 29)")
                    .set_theme_color("#e53935")
                    .add_icon("/icon", "1024x1024", "image/png")
                    .set_start_url("/")
                    .set_display(web_base::ManifestDisplay::MinimalUI)
                    .set_description("watch & share videos")
            ),
        |app: actix_web::App<_>| {
            // Base
            app.app_data(library.clone())
                .service(pages::index::index)
                .service(pages::index::channel_page)
                .service(pages::index::search)
                // Videos
                .service(pages::video::video_page)
                // Assets
                .service(pages::assets::icon_resource)
                .service(pages::assets::video_file)
                .service(pages::assets::video_thumbnail)
                // YT Pages
                .service(pages::yt::yt_channel_page)
                .service(pages::yt::yt_tags)
                .service(pages::yt::yt_tag_page)
        }
    )
    .bind(("0.0.0.0".to_string(), 8080))?
    .run()
    .await
}
