#[cfg(test)]
mod tests {
  use clap::CommandFactory;
  use clap::error::ErrorKind;

  use crate::Cli;
  use crate::cli::Command;
  use crate::cli::radarr::RadarrCommand;
  use crate::cli::radarr::list_command_handler::RadarrListCommand;
  use pretty_assertions::assert_eq;

  #[test]
  fn test_radarr_list_command_from() {
    let command = RadarrListCommand::Movies;

    let result = Command::from(command.clone());

    assert_eq!(result, Command::Radarr(RadarrCommand::List(command)));
  }

  mod cli {
    use super::*;
    use clap::Parser;
    use pretty_assertions::assert_eq;
    use rstest::rstest;

    #[rstest]
    fn test_list_commands_have_no_arg_requirements(
      #[values(
        "blocklist",
        "collections",
        "disk-space",
        "indexers",
        "movies",
        "quality-profiles",
        "queued-events",
        "root-folders",
        "tags",
        "tasks",
        "updates"
      )]
      subcommand: &str,
    ) {
      let result = Cli::command().try_get_matches_from(["managarr", "radarr", "list", subcommand]);

      assert_ok!(&result);
    }

    #[test]
    fn test_list_movie_credits_requires_movie_id() {
      let result =
        Cli::command().try_get_matches_from(["managarr", "radarr", "list", "movie-credits"]);

      assert_err!(&result);
      assert_eq!(
        result.unwrap_err().kind(),
        ErrorKind::MissingRequiredArgument
      );
    }

    #[test]
    fn test_list_downloads_count_flag_requires_arguments() {
      let result =
        Cli::command().try_get_matches_from(["managarr", "radarr", "list", "downloads", "--count"]);

      assert_err!(&result);
      assert_eq!(result.unwrap_err().kind(), ErrorKind::InvalidValue);
    }

    #[test]
    fn test_list_logs_events_flag_requires_arguments() {
      let result =
        Cli::command().try_get_matches_from(["managarr", "radarr", "list", "logs", "--events"]);

      assert_err!(&result);
      assert_eq!(result.unwrap_err().kind(), ErrorKind::InvalidValue);
    }

    #[test]
    fn test_list_movie_credits_success() {
      let expected_args = RadarrListCommand::MovieCredits { movie_id: 1 };
      let result = Cli::try_parse_from([
        "managarr",
        "radarr",
        "list",
        "movie-credits",
        "--movie-id",
        "1",
      ]);

      assert_ok!(&result);

      let Some(Command::Radarr(RadarrCommand::List(credits_command))) = result.unwrap().command
      else {
        panic!("Unexpected command type");
      };
      assert_eq!(credits_command, expected_args);
    }

    #[test]
    fn test_list_downloads_default_values() {
      let expected_args = RadarrListCommand::Downloads { count: 500 };
      let result = Cli::try_parse_from(["managarr", "radarr", "list", "downloads"]);

      assert_ok!(&result);

      let Some(Command::Radarr(RadarrCommand::List(refresh_command))) = result.unwrap().command
      else {
        panic!("Unexpected command type");
      };
      assert_eq!(refresh_command, expected_args);
    }

    #[test]
    fn test_list_history_events_flag_requires_arguments() {
      let result =
        Cli::command().try_get_matches_from(["managarr", "radarr", "list", "history", "--events"]);

      assert_err!(&result);
      assert_eq!(result.unwrap_err().kind(), ErrorKind::InvalidValue);
    }

    #[test]
    fn test_list_history_default_values() {
      let expected_args = RadarrListCommand::History { events: 500 };
      let result = Cli::try_parse_from(["managarr", "radarr", "list", "history"]);

      assert_ok!(&result);

      let Some(Command::Radarr(RadarrCommand::List(history_command))) = result.unwrap().command
      else {
        panic!("Unexpected command type");
      };
      assert_eq!(history_command, expected_args);
    }

    #[test]
    fn test_list_logs_default_values() {
      let expected_args = RadarrListCommand::Logs {
        events: 500,
        output_in_log_format: false,
      };
      let result = Cli::try_parse_from(["managarr", "radarr", "list", "logs"]);

      assert_ok!(&result);

      let Some(Command::Radarr(RadarrCommand::List(refresh_command))) = result.unwrap().command
      else {
        panic!("Unexpected command type");
      };
      assert_eq!(refresh_command, expected_args);
    }
  }

  mod handler {
    use std::sync::Arc;

    use mockall::predicate::eq;
    use rstest::rstest;
    use serde_json::json;
    use tokio::sync::Mutex;

    use crate::cli::CliCommandHandler;
    use crate::{
      app::App,
      cli::radarr::list_command_handler::{RadarrListCommand, RadarrListCommandHandler},
      models::{Serdeable, radarr_models::RadarrSerdeable},
      network::{MockNetworkTrait, NetworkEvent, radarr_network::RadarrEvent},
    };

    #[rstest]
    #[case(RadarrListCommand::Blocklist, RadarrEvent::GetBlocklist)]
    #[case(RadarrListCommand::Collections, RadarrEvent::GetCollections)]
    #[case(RadarrListCommand::DiskSpace, RadarrEvent::GetDiskSpace)]
    #[case(RadarrListCommand::Indexers, RadarrEvent::GetIndexers)]
    #[case(RadarrListCommand::Movies, RadarrEvent::GetMovies)]
    #[case(RadarrListCommand::QualityProfiles, RadarrEvent::GetQualityProfiles)]
    #[case(RadarrListCommand::QueuedEvents, RadarrEvent::GetQueuedEvents)]
    #[case(RadarrListCommand::RootFolders, RadarrEvent::GetRootFolders)]
    #[case(RadarrListCommand::Tags, RadarrEvent::GetTags)]
    #[case(RadarrListCommand::Tasks, RadarrEvent::GetTasks)]
    #[case(RadarrListCommand::Updates, RadarrEvent::GetUpdates)]
    #[tokio::test]
    async fn test_handle_list_command(
      #[case] list_command: RadarrListCommand,
      #[case] expected_radarr_event: RadarrEvent,
    ) {
      let mut mock_network = MockNetworkTrait::new();
      mock_network
        .expect_handle_network_event()
        .with(eq::<NetworkEvent>(expected_radarr_event.into()))
        .times(1)
        .returning(|_| {
          Ok(Serdeable::Radarr(RadarrSerdeable::Value(
            json!({"testResponse": "response"}),
          )))
        });
      let app_arc = Arc::new(Mutex::new(App::test_default()));

      let result = RadarrListCommandHandler::with(&app_arc, list_command, &mut mock_network)
        .handle()
        .await;

      assert_ok!(&result);
    }

    #[tokio::test]
    async fn test_handle_list_movie_credits_command() {
      let expected_movie_id = 1;
      let mut mock_network = MockNetworkTrait::new();
      mock_network
        .expect_handle_network_event()
        .with(eq::<NetworkEvent>(
          RadarrEvent::GetMovieCredits(expected_movie_id).into(),
        ))
        .times(1)
        .returning(|_| {
          Ok(Serdeable::Radarr(RadarrSerdeable::Value(
            json!({"testResponse": "response"}),
          )))
        });
      let app_arc = Arc::new(Mutex::new(App::test_default()));
      let list_movie_credits_command = RadarrListCommand::MovieCredits { movie_id: 1 };

      let result =
        RadarrListCommandHandler::with(&app_arc, list_movie_credits_command, &mut mock_network)
          .handle()
          .await;

      assert_ok!(&result);
    }

    #[tokio::test]
    async fn test_handle_list_downloads_command() {
      let expected_count = 1000;
      let mut mock_network = MockNetworkTrait::new();
      mock_network
        .expect_handle_network_event()
        .with(eq::<NetworkEvent>(
          RadarrEvent::GetDownloads(expected_count).into(),
        ))
        .times(1)
        .returning(|_| {
          Ok(Serdeable::Radarr(RadarrSerdeable::Value(
            json!({"testResponse": "response"}),
          )))
        });
      let app_arc = Arc::new(Mutex::new(App::test_default()));
      let list_downloads_command = RadarrListCommand::Downloads { count: 1000 };

      let result =
        RadarrListCommandHandler::with(&app_arc, list_downloads_command, &mut mock_network)
          .handle()
          .await;

      assert_ok!(&result);
    }

    #[tokio::test]
    async fn test_handle_list_history_command() {
      let expected_events = 1000;
      let mut mock_network = MockNetworkTrait::new();
      mock_network
        .expect_handle_network_event()
        .with(eq::<NetworkEvent>(
          RadarrEvent::GetHistory(expected_events).into(),
        ))
        .times(1)
        .returning(|_| {
          Ok(Serdeable::Radarr(RadarrSerdeable::Value(
            json!({"testResponse": "response"}),
          )))
        });
      let app_arc = Arc::new(Mutex::new(App::test_default()));
      let list_history_command = RadarrListCommand::History { events: 1000 };

      let result =
        RadarrListCommandHandler::with(&app_arc, list_history_command, &mut mock_network)
          .handle()
          .await;

      assert_ok!(&result);
    }

    #[tokio::test]
    async fn test_handle_list_logs_command() {
      let expected_events = 1000;
      let mut mock_network = MockNetworkTrait::new();
      mock_network
        .expect_handle_network_event()
        .with(eq::<NetworkEvent>(
          RadarrEvent::GetLogs(expected_events).into(),
        ))
        .times(1)
        .returning(|_| {
          Ok(Serdeable::Radarr(RadarrSerdeable::Value(
            json!({"testResponse": "response"}),
          )))
        });
      let app_arc = Arc::new(Mutex::new(App::test_default()));
      let list_logs_command = RadarrListCommand::Logs {
        events: 1000,
        output_in_log_format: false,
      };

      let result = RadarrListCommandHandler::with(&app_arc, list_logs_command, &mut mock_network)
        .handle()
        .await;

      assert_ok!(&result);
    }
  }
}
