extern crate core;
#[macro_use]
extern crate rocket;

use dotenv_codegen::dotenv;
use rocket_cors::{CorsOptions};
use sqlx::mysql::MySqlPoolOptions;

use controllers::{auth_controller, movie_controller, movielist_controller, schedule_controller};

mod controllers;
mod models;
mod movie_builder;
mod requests;
mod responses;
mod traits;

#[rocket::main]
async fn main() -> Result<(), rocket::Error> {
    let connection_url = &format!(
        "{}:{}/{}",
        dotenv!("DATABASE_URL"),
        dotenv!("DATABASE_PORT"),
        dotenv!("DATABASE_NAME"),
    );

    let pool = MySqlPoolOptions::new()
        .max_connections(5)
        .connect(connection_url)
        .await
        .expect("Error connecting to database");

    let cors = CorsOptions::default()
        .allow_credentials(true);

    let _rocket = rocket::build()
        .mount("/api", routes![movie_controller::get_movie])
        .mount("/api", routes![movie_controller::get_movies])
        .mount("/api", routes![movie_controller::remove_movie])
        .mount("/api", routes![movielist_controller::create_list])
        .mount("/api", routes![movielist_controller::delete_list])
        .mount("/api", routes![movielist_controller::get_list])
        .mount("/api", routes![movielist_controller::get_lists])
        .mount("/api", routes![movielist_controller::add_movie])
        .mount("/api", routes![schedule_controller::create_schedule])
        .mount("/api", routes![schedule_controller::add_movie])
        .mount("/api", routes![schedule_controller::get_schedule])
        .mount("/api", routes![schedule_controller::delete_showing])
        .mount("/api", routes![auth_controller::login])
        .mount("/api", routes![auth_controller::logout])
        .mount("/api", routes![auth_controller::create_account])
        .mount("/api", routes![auth_controller::reset_password])
        .mount("/api", routes![movie_controller::search])
        .attach(cors.to_cors().unwrap())
        .manage(pool)
        .launch()
        .await?;
    Ok(())
}
