use crate::models::DBVideo;
use diesel::prelude::*;

pub fn get_video(
    conn: &mut SqliteConnection,
    video_id: String,
) -> Result<Option<String>, diesel::result::Error> {
    use crate::schema::videos::dsl::*;
    let result = videos
        .filter(id.eq(video_id))
        .first::<DBVideo>(conn)
        .optional()?;
    match result {
        Some(video) => Ok(Some(video.url)),
        None => Ok(None),
    }
}

pub fn insert_video(
    conn: &mut SqliteConnection,
    new_id: &str,
    new_url: &str,
) -> Result<usize, diesel::result::Error> {
    use crate::schema::videos::dsl::*;
    diesel::insert_into(videos)
        .values(DBVideo {
            id: new_id.to_string(),
            url: new_url.to_string(),
        })
        .execute(conn)
}

pub fn delete_video(
    conn: &mut SqliteConnection,
    video_id: String,
) -> Result<usize, diesel::result::Error> {
    use crate::schema::videos::dsl::*;
    diesel::delete(videos.filter(id.eq(video_id))).execute(conn)
}

pub fn has_table(
    conn: &mut SqliteConnection,
    table_name: &str,
) -> Result<bool, diesel::result::Error> {
    use diesel::sql_query;
    use diesel::sql_types::Text;
    #[derive(QueryableByName)]
    struct TableName {
        #[diesel(sql_type = Text)]
        #[diesel(column_name = name)]
        name: String,
    }

    let query = "SELECT name FROM sqlite_master WHERE type='table' AND name = ?1";
    let rows = sql_query(query)
        .bind::<Text, _>(table_name)
        .load::<TableName>(conn)?;
    let exists = rows.first().map(|r| !r.name.is_empty()).unwrap_or(false);
    Ok(exists)
}

pub fn create_table(
    conn: &mut SqliteConnection,
    create_sql: &str,
) -> Result<(), diesel::result::Error> {
    use diesel::sql_query;
    sql_query(create_sql).execute(conn)?;
    Ok(())
}
