use super::{
    Database, DatabaseError,
    entities::{NewShow, Show, ShowWithCover},
    queries,
};
use chrono::Utc;
use rusqlite::params;

impl Database {
    db_query_all!(fn find_unmatched_shows() -> Vec<Show>, queries::find_unmatched_shows);
    db_query_all!(fn find_matched_shows() -> Vec<Show>, queries::find_matched_shows);
    db_query_all!(fn find_shows_with_covers() -> Vec<ShowWithCover>, queries::find_shows_with_covers);

    db_query_opt!(fn find_show_by_name(title: &str) -> Option<Show>, queries::find_show_by_name);

    db_exec!(fn delete_show_images(show_id: i64, image_type: &str), queries::delete_show_images);
    db_exec!(fn link_show_image(show_id: i64, image_id: i64, image_type: &str), queries::link_show_image);
    db_exec!(fn set_show_skip_auto_match(id: i64), queries::set_show_skip_auto_match);
    db_exec!(fn reset_show_auto_match(), queries::reset_show_auto_match);
    //db_exec!(fn set_show_watched(id: i64, watched: bool), queries::set_show_watched);

    pub fn create_show(&mut self, new_show: NewShow) -> Result<i64, DatabaseError> {
        let now = Utc::now();
        let (_, insert_id) = self.exec_cmd(
            &queries::create_show,
            params![
                Option::<i64>::None,
                new_show.title,
                Option::<String>::None,
                Option::<i64>::None,
                now,
                now
            ],
        )?;

        Ok(insert_id)
    }

    pub fn update_show_tmdb(
        &mut self,
        id: i64,
        tmdb_id: i64,
        original_title: &str,
        release_year: Option<i64>,
        description: Option<&str>,
        rating: Option<f64>,
    ) -> Result<(), DatabaseError> {
        self.exec_cmd(
            &queries::update_show_tmdb,
            params![
                tmdb_id,
                original_title,
                release_year,
                description,
                rating,
                id
            ],
        )?;
        Ok(())
    }
}
