/*
 *  Copyright (C) 2016-2025 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#pragma once

#include "Bookmark.h"
#include "VideoInfoTag.h"
#include "addons/Scraper.h"
#include "dbwrappers/Database.h"
#include "utils/Artwork.h"
#include "utils/SortUtils.h"
#include "utils/UrlOptions.h"

#include <array>
#include <functional>
#include <memory>
#include <set>
#include <stdexcept>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include <vector>

#include <fmt/format.h>

class CFileItem;
class CFileItemList;
class CStreamDetails;
class CVideoSettings;
class CGUIDialogProgress;
class CGUIDialogProgressBarHandle;
class TiXmlNode;

struct VideoAssetInfo;

enum class VideoAssetTypeOwner;
enum class VideoAssetType;

namespace dbiplus
{
  class field_value;
  using sql_record = std::vector<field_value>;
}

namespace KODI::VIDEO
{
  class IVideoInfoScannerObserver;
  struct SScanSettings;
}

enum VideoDbDetails
{
  VideoDbDetailsNone     = 0x00,
  VideoDbDetailsRating   = 0x01,
  VideoDbDetailsTag      = 0x02,
  VideoDbDetailsShowLink = 0x04,
  VideoDbDetailsStream   = 0x08,
  VideoDbDetailsCast     = 0x10,
  VideoDbDetailsBookmark = 0x20,
  VideoDbDetailsUniqueID = 0x40,
  VideoDbDetailsAll      = 0xFF
} ;

enum class VideoDbContentType
{
  UNKNOWN = -1,
  MOVIES = 1,
  TVSHOWS = 2,
  MUSICVIDEOS = 3,
  EPISODES = 4,
  MOVIE_SETS = 5,
  MUSICALBUMS = 6
};

template<>
struct fmt::formatter<VideoDbContentType> : fmt::formatter<std::string_view>
{
  template<typename FormatContext>
  constexpr auto format(const VideoDbContentType& type, FormatContext& ctx)
  {
    return fmt::formatter<std::string_view>::format(enumToSV(type), ctx);
  }

private:
  static constexpr std::string_view enumToSV(VideoDbContentType type)
  {
    using namespace std::literals::string_view_literals;
    switch (type)
    {
      using enum VideoDbContentType;

      case UNKNOWN:
        return "unknown"sv;
      case MOVIES:
        return "movies"sv;
      case TVSHOWS:
        return "TV shows"sv;
      case MUSICVIDEOS:
        return "music videos"sv;
      case EPISODES:
        return "episodes"sv;
      case MOVIE_SETS:
        return "movie sets"sv;
      case MUSICALBUMS:
        return "music albums"sv;
    };
    throw std::invalid_argument("no videodb content string found");
  }
};

enum class ArtFallbackOptions
{
  NONE,
  PARENT
};

enum class DeleteMovieCascadeAction
{
  DEFAULT_VERSION,
  ALL_ASSETS,
  ALL_ASSETS_NOT_STREAMDETAILS
};

enum class DeleteMovieHashAction
{
  HASH_DELETE,
  HASH_PRESERVE
};

#define COMPARE_PERCENTAGE     0.90f // 90%
#define COMPARE_PERCENTAGE_MIN 0.50f // 50%

struct EpisodeInformation
{
  int index{0};
  int season{-1};
  int episode{-1};
  unsigned int duration{0};
  CBookmark bookmark{};
};

using EpisodeFileMap = std::multimap<std::string, EpisodeInformation, std::less<>>;
using EpisodeFileMapEntry = EpisodeFileMap::value_type;

static constexpr const char* MULTIPLE_EPISODES{"multiple_episodes"};

class CVideoDatabase : public CDatabase
{
  struct FileInformation
  {
    // user-defined ctor required for XCode 15.2 and emplace_back
    FileInformation(std::string&& newPath, int newFileId, int newVvId, std::string&& newHash);

    std::string path;
    int fileId{0};
    int vvId{0};
    std::string hash;
  };

public:

  class CActor    // used for actor retrieval for non-master users
  {
  public:
    std::string name;
    std::string thumb;
    int playcount;
    int appearances;
  };

  class CSeason   // used for season retrieval for non-master users
  {
  public:
    std::string path;
    std::vector<std::string> genre;
    int numEpisodes;
    int numWatched;
    int id;
  };

  class CSetInfo
  {
  public:
    std::string name;
    std::vector<CVideoInfoTag> movies;
    DatabaseResults results;
  };

  CVideoDatabase();
  ~CVideoDatabase() override;

  bool Open() override;
  bool CommitTransaction() override;

  int AddNewEpisode(int idShow, CVideoInfoTag& details);

  // editing functions
  /*! \brief Set the playcount of an item, update last played time
   Sets the playcount and last played date to a given value
   \param item CFileItem to set the playcount for
   \param count The playcount to set.
   \param date The date the file was last viewed (does not denote the video was watched to completion).
   If empty we use current datetime (if count > 0) or never viewed (if count = 0).
   \return on success, the new last played time set, invalid datetime otherwise.
   \sa GetPlayCount, IncrementPlayCount, UpdateLastPlayed
   */
  CDateTime SetPlayCount(const CFileItem& item, int count, const CDateTime& date = CDateTime());

  /*! \brief Increment the playcount of an item
   Increments the playcount and updates the last played date
   \param item CFileItem to increment the playcount for
   \return on success, the new last played time set, invalid datetime otherwise.
   \sa GetPlayCount, SetPlayCount, GetPlayCounts
   */
  CDateTime IncrementPlayCount(const CFileItem& item);

  /*! \brief Get the playcount of an item
   \param item CFileItem to get the playcount for
   \return the playcount of the item, or -1 on error
   \sa SetPlayCount, IncrementPlayCount, GetPlayCounts
   */
  int GetPlayCount(const CFileItem &item);

  /*! \brief Get the playcount of a filename and path
   \param strFilenameAndPath filename and path to get the playcount for
   \return the playcount of the item, or -1 on error
   \sa SetPlayCount, IncrementPlayCount, GetPlayCounts
   */
  int GetPlayCount(const std::string& strFilenameAndPath);

  /*! \brief Get the last played time of a filename and path
   \param strFilenameAndPath filename and path to get the last played time for
   \return the last played time of the item, or an invalid CDateTime on error
   \sa UpdateLastPlayed
   */
  CDateTime GetLastPlayed(const std::string& strFilenameAndPath);

  /*! \brief Update the last played time of an item
   Updates the last played date
   \param item CFileItem to update the last played time for
   \return on success, the last played time set, invalid datetime otherwise.
   \sa GetPlayCount, SetPlayCount, IncrementPlayCount, GetPlayCounts
   */
  CDateTime UpdateLastPlayed(const CFileItem& item);

  /*! \brief Get the playcount and resume point of a list of items
   Note that if the resume point is already set on an item, it won't be overridden.
   \param path the path to fetch videos from
   \param items CFileItemList to fetch the playcounts for
   \sa GetPlayCount, SetPlayCount, IncrementPlayCount
   */
  bool GetPlayCounts(const std::string &path, CFileItemList &items);

  void UpdateMovieTitle(int idMovie,
                        const std::string& strNewMovieTitle,
                        VideoDbContentType iType = VideoDbContentType::MOVIES);
  bool UpdateVideoSortTitle(int idDb,
                            const std::string& strNewSortTitle,
                            VideoDbContentType iType = VideoDbContentType::MOVIES);

  bool HasMovieInfo(const std::string& strFilenameAndPath);
  bool HasTvShowInfo(const std::string& strFilenameAndPath);
  bool HasEpisodeInfo(const std::string& strFilenameAndPath);
  bool HasMusicVideoInfo(const std::string& strFilenameAndPath);

  void GetFilePathById(int idMovie, std::string& filePath, VideoDbContentType iType);
  std::string GetGenreById(int id) const;
  std::string GetCountryById(int id) const;
  std::string GetSetById(int id) const;
  std::string GetOriginalSetById(int id) const;
  std::string GetTagById(int id) const;
  std::string GetPersonById(int id) const;
  std::string GetStudioById(int id) const;
  std::string GetTvShowTitleById(int id) const;
  std::string GetMusicVideoAlbumById(int id) const;
  int GetTvShowForEpisode(int idEpisode) const;
  int GetSeasonForEpisode(int idEpisode) const;

  bool LoadVideoInfo(const std::string& strFilenameAndPath, CVideoInfoTag& details);
  bool GetMovieInfo(const std::string& strFilenameAndPath,
                    CVideoInfoTag& details,
                    int idMovie = -1,
                    int idVersion = -1,
                    int idFile = -1,
                    int getDetails = VideoDbDetailsAll);
  bool GetTvShowInfo(const std::string& strPath,
                     CVideoInfoTag& details,
                     int idTvShow = -1,
                     CFileItem* item = nullptr,
                     int getDetails = VideoDbDetailsAll);
  bool GetSeasonInfo(const std::string& path, int season, CVideoInfoTag& details, CFileItem* item);
  bool GetSeasonInfo(int idSeason, CVideoInfoTag& details, CFileItem* item);
  bool GetSeasonInfo(int idSeason, CVideoInfoTag& details, bool allDetails = true);
  bool GetEpisodeBasicInfo(const std::string& strFilenameAndPath, CVideoInfoTag& details, int idEpisode  = -1);
  bool GetEpisodeInfo(const std::string& strFilenameAndPath, CVideoInfoTag& details, int idEpisode = -1, int getDetails = VideoDbDetailsAll);
  bool GetMusicVideoInfo(const std::string& strFilenameAndPath, CVideoInfoTag& details, int idMVideo = -1, int getDetails = VideoDbDetailsAll);
  bool GetSetInfo(int idSet, CVideoInfoTag& details, CFileItem* item = nullptr);
  bool GetFileInfo(const std::string& strFilenameAndPath, CVideoInfoTag& details, int idFile = -1);

  int GetPathId(const std::string& strPath);
  int GetTvShowId(const std::string& strPath);
  int GetEpisodeId(const std::string& strFilenameAndPath, int idEpisode=-1, int idSeason=-1); // idEpisode, idSeason are used for multipart episodes as hints
  int GetSeasonId(int idShow, int season) const;

  void GetEpisodesByBlurayPath(const std::string& path, std::vector<CVideoInfoTag>& episodes);
  void GetEpisodesByBasePath(const std::string& path,
                             std::vector<CVideoInfoTag>& episodes,
                             int idShow = -1);
  void GetEpisodesByFile(const std::string& strFilenameAndPath, std::vector<CVideoInfoTag>& episodes);
  void GetEpisodesByFileId(int idFile, std::vector<CVideoInfoTag>& episodes);
  bool GetEpisodeMap(int idShow, EpisodeFileMap& fileMap, int idFile = -1) const;
  bool GetEpisodeMap(int idShow,
                     EpisodeFileMap& fileMap,
                     dbiplus::Dataset& pDS,
                     int idFile = -1 /* = -1 */) const;

  int SetDetailsForMovie(CVideoInfoTag& details,
                         const KODI::ART::Artwork& artwork,
                         int idMovie = -1);
  int SetDetailsForMovieSet(const CVideoInfoTag& details,
                            const KODI::ART::Artwork& artwork,
                            int idSet = -1);

  /*! \brief add a tvshow to the library, setting metadata detail
   First checks for whether this TV Show is already in the database (based on idTvShow, or via GetMatchingTvShow)
   and if present adds the paths to the show.  If not present, we add a new show and set the show metadata.
   \param paths a vector<string,string> list of the path(s) and parent path(s) for the show.
   \param details a CVideoInfoTag filled with the metadata for the show.
   \param artwork the artwork map for the show.
   \param seasonArt the artwork map for seasons.
   \param idTvShow the database id of the tvshow if known (defaults to -1)
   \return the id of the tvshow.
   */
  int SetDetailsForTvShow(const std::vector<std::string>& paths,
                          CVideoInfoTag& details,
                          const KODI::ART::Artwork& artwork,
                          const KODI::ART::SeasonsArtwork& seasonArt,
                          int idTvShow = -1);
  bool UpdateDetailsForTvShow(int idTvShow,
                              CVideoInfoTag& details,
                              const KODI::ART::Artwork& artwork,
                              const KODI::ART::SeasonsArtwork& seasonArt);
  int SetDetailsForSeason(const CVideoInfoTag& details,
                          const KODI::ART::Artwork& artwork,
                          int idShow,
                          int idSeason = -1);
  int SetDetailsForEpisode(CVideoInfoTag& details,
                           const KODI::ART::Artwork& artwork,
                           int idShow,
                           int idEpisode = -1);

  /*!
   * @brief The FileInfo structure represents a DB record of the files table.
   */
  struct FileRecord
  {
    int m_idFile{-1};
    int m_playCount{-1};
    CDateTime m_lastPlayed{};
    CDateTime m_dateAdded{};
  };

  int SetFileForMedia(const std::string& fileAndPath,
                      VideoDbContentType type,
                      int mediaId,
                      const FileRecord& oldFile);

  int SetDetailsForMusicVideo(CVideoInfoTag& details,
                              const KODI::ART::Artwork& artwork,
                              int idMVideo = -1);
  bool SetStreamDetailsForFile(const CStreamDetails& details,
                               const std::string& strFileNameAndPath);

  /*!
   * \brief Clear any existing stream details and add the new provided details to a file.
   * \param[in] details New stream details
   * \param[in] idFile Identifier of the file
   * \return operation success. true for success, false for failure
   */
  bool SetStreamDetailsForFileId(const CStreamDetails& details, int idFile);

  struct PlaylistInfo
  {
    int playlist{-1};
    int idFile{-1};
    VideoDbContentType mediaType{-1};
    int idMedia{-1};
  };

  /*!
   * \brief Get all playlists from a single bluray:// path in the database
   * \param[in] path The bluray:// path
   * \return vector array of playlist numbers and idFiles
   */
  std::vector<PlaylistInfo> GetPlaylistsByPath(const std::string& path);

  void SetTrailerForMovie(int idMovie, const std::string& trailer);

  bool SetSingleValue(VideoDbContentType type, int dbId, int dbField, const std::string& strValue);
  bool SetSingleValue(VideoDbContentType type,
                      int dbId,
                      Field dbField,
                      const std::string& strValue);
  bool SetSingleValue(const std::string &table, const std::string &fieldName, const std::string &strValue,
                      const std::string &conditionName = "", int conditionValue = -1);

  int UpdateDetailsForMovie(int idMovie,
                            CVideoInfoTag& details,
                            const KODI::ART::Artwork& artwork,
                            const std::set<std::string, std::less<>>& updatedDetails);

  /*!
   * \brief Remove a movie from the library.
   * \param[in] idMovie The id of the movie
   * \param[in] action Versions of the movie to be deleted
   * \param[in] hashAction Preserve or invalidate the hash of the movie path
   * \return operation success. true for success, false for failure
   */
  bool DeleteMovie(int idMovie,
                   DeleteMovieCascadeAction action = DeleteMovieCascadeAction::ALL_ASSETS,
                   DeleteMovieHashAction hashAction = DeleteMovieHashAction::HASH_DELETE);
  void DeleteTvShow(int idTvShow, bool bKeepId = false);
  void DeleteTvShow(const std::string& strPath);
  void DeleteSeason(int idSeason, bool bKeepId = false);
  void DeleteEpisode(int idEpisode, bool bKeepId = false);
  void DeleteMusicVideo(int idMusicVideo, bool bKeepId = false);
  void DeleteDetailsForTvShow(int idTvShow);
  void DeleteStreamDetails(int idFile);
  void RemoveContentForPath(const std::string& strPath, CGUIDialogProgress* progress = nullptr);
  void UpdateFanart(const CFileItem& item, VideoDbContentType type);
  void DeleteSet(int idSet);
  void DeleteTag(int idTag, VideoDbContentType mediaType);
  bool DeleteFile(int idFile);

  /*! \brief Get video settings for the specified file id
   \param idFile file id to get the settings for
   \return true if video settings found, false otherwise
   \sa SetVideoSettings
   */
  bool GetVideoSettings(int idFile, CVideoSettings &settings);

  /*! \brief Get video settings for the specified file item
   \param item item to get the settings for
   \return true if video settings found, false otherwise
   \sa SetVideoSettings
   */
  bool GetVideoSettings(const CFileItem &item, CVideoSettings &settings);

  /*! \brief Get video settings for the specified file path
   \param filePath filepath to get the settings for
   \return true if video settings found, false otherwise
   \sa SetVideoSettings
   */
  bool GetVideoSettings(const std::string &filePath, CVideoSettings &settings);

  /*! \brief Set video settings for the specified file path
   \param fileItem to set the settings for
   \sa GetVideoSettings
   */
  void SetVideoSettings(const CFileItem &item, const CVideoSettings &settings);

  /*! \brief Set video settings for the specified file path
   \param fileId to set the settings for
   \sa GetVideoSettings
   */
  void SetVideoSettings(int idFile, const CVideoSettings &settings);

  /**
   * Erases video settings for file item
   * @param fileitem
   */
  void EraseVideoSettings(const CFileItem &item);

  /**
   * Erases all video settings
   */
  void EraseAllVideoSettings();

  /**
   * Erases video settings for files starting with path
   * @param path pattern
   */
  void EraseAllVideoSettings(const std::string& path);

  /**
   * Erases all entries for files starting with path, including the files and path entries
   * @param path pattern
   */
  void EraseAllForPath(const std::string& path);

  /*! \brief Erases all entries for the given file, including path entry if no longer used.
   \param fileNameAndPath The name and path of the file to erase db entries for.
   \return True on success, false otherwise.
   */
  bool EraseAllForFile(const std::string& fileNameAndPath);

  bool GetStackTimes(const std::string& filePath, std::vector<std::chrono::milliseconds>& times);
  void SetStackTimes(const std::string& filePath,
                     const std::vector<std::chrono::milliseconds>& times);

  void GetBookMarksForFile(const std::string& strFilenameAndPath, VECBOOKMARKS& bookmarks, CBookmark::EType type = CBookmark::STANDARD, bool bAppend=false, long partNumber=0);
  bool AddBookMarkToFile(const std::string& strFilenameAndPath,
                         const CBookmark& bookmark,
                         CBookmark::EType type = CBookmark::STANDARD);
  bool GetResumeBookMark(const std::string& strFilenameAndPath, CBookmark &bookmark);
  void DeleteResumeBookMark(const CFileItem& item);
  void ClearBookMarkOfFile(const std::string& strFilenameAndPath,
                           const CBookmark& bookmark,
                           CBookmark::EType type = CBookmark::STANDARD);
  bool ClearBookMarksOfFile(const std::string& strFilenameAndPath,
                            CBookmark::EType type = CBookmark::STANDARD);
  bool ClearBookMarksOfFile(int idFile, CBookmark::EType type = CBookmark::STANDARD);
  bool GetBookMarkForEpisode(int dbId, CBookmark& bookmark) const;
  bool GetBookMarkForEpisode(const CVideoInfoTag& tag, CBookmark& bookmark) const;
  void AddBookMarkForEpisode(const CVideoInfoTag& tag, const CBookmark& bookmark);
  void DeleteBookMarkForEpisode(const CVideoInfoTag& tag);
  bool GetResumePoint(CVideoInfoTag& tag);
  bool GetStreamDetails(CFileItem& item);
  bool GetStreamDetails(CVideoInfoTag& tag);
  bool GetStreamDetails(const std::string& filenameAndPath, CStreamDetails& details);
  bool GetDetailsByTypeAndId(CFileItem& item, VideoDbContentType type, int id);
  CVideoInfoTag GetDetailsByTypeAndId(VideoDbContentType type, int id);

  // scraper settings
  struct StringHash
  {
    using is_transparent = void; // Enables heterogeneous operations.
    std::size_t operator()(std::string_view sv) const
    {
      std::hash<std::string_view> hasher;
      return hasher(sv);
    }
  };
  using ScraperCache =
      std::unordered_map<std::string, ADDON::ScraperPtr, StringHash, std::equal_to<>>;
  void SetScraperForPath(const std::string& filePath,
                         const ADDON::ScraperPtr& info,
                         const KODI::VIDEO::SScanSettings& settings);
  ADDON::ScraperPtr GetScraperForPath(const std::string& strPath,
                                      ScraperCache* scraperCache = nullptr);
  ADDON::ScraperPtr GetScraperForPath(const std::string& strPath,
                                      KODI::VIDEO::SScanSettings& settings,
                                      ScraperCache* scraperCache = nullptr);

  /*! \brief Retrieve the scraper and settings we should use for the specified path
   If the scraper is not set on this particular path, we'll recursively check parent folders.
   \param strPath path to start searching in.
   \param settings [out] scan settings for this folder.
   \param foundDirectly [out] true if a scraper was found directly for strPath, false if it was in a parent path.
   \return A ScraperPtr containing the scraper information. Returns nullptr if a trivial (Content == CONTENT_NONE)
           scraper or no scraper is found.
   */
  ADDON::ScraperPtr GetScraperForPath(const std::string& strPath,
                                      KODI::VIDEO::SScanSettings& settings,
                                      bool& foundDirectly,
                                      ScraperCache* scraperCache = nullptr);

  /*! \brief Retrieve the content type of videos in the given path
   If content is set on the folder, we return the given content type, except in the case of tvshows,
   where we first check for whether we have episodes directly in the path (thus return episodes) or whether
   we've found a scraper directly (shows).  Any folders inbetween are treated as seasons (regardless of whether
   they actually are seasons). Note that any subfolders in movies will be treated as movies.
   \param strPath path to start searching in.
   \return A content type string for the current path.
   */
  std::string GetContentForPath(const std::string& strPath);

  /*! \brief Get videos of the given content type from the given path
   \param content the content type to fetch.
   \param path the path to fetch videos from.
   \param items the returned items
   \return true if items are found, false otherwise.
   */
  bool GetItemsForPath(const std::string &content, const std::string &path, CFileItemList &items);

  /*! \brief Check whether a given scraper is in use.
   \param scraperID the scraper to check for.
   \return true if the scraper is in use, false otherwise.
   */
  bool ScraperInUse(const std::string &scraperID) const;

  // scanning hashes and paths scanned
  bool SetPathHash(const std::string &path, const std::string &hash);
  bool GetPathHash(const std::string &path, std::string &hash);
  bool GetPaths(std::set<std::string, std::less<>>& paths);
  bool GetPathsForTvShow(int idShow, std::set<int>& paths);

  /*! \brief return the paths linked to a tvshow.
   \param idShow the id of the tvshow.
   \param paths [out] the list of paths associated with the show.
   \return true on success, false on failure.
   */
  bool GetPathsLinkedToTvShow(int idShow, std::vector<std::string> &paths);

  /*! \brief retrieve subpaths of a given path.  Assumes a hierarchical folder structure
   \param basepath the root path to retrieve subpaths for
   \param subpaths the returned subpaths
   \param excludeDiscPaths true - exclude disc paths that contain VIDEO_TS.IFO or INDEX.BDMV
                             (default)
                           false - include encoded paths for cleaning (eg. bluray://, zip:// etc.)
   \return true if we successfully retrieve subpaths (may be zero), false on error
   */
  bool GetSubPaths(const std::string& basepath,
                   std::vector<std::pair<int, std::string>>& subpaths,
                   bool excludeDiscPaths = true);

  bool GetSourcePath(const std::string &path, std::string &sourcePath);
  bool GetSourcePath(const std::string& path,
                     std::string& sourcePath,
                     KODI::VIDEO::SScanSettings& settings);

  // for music + musicvideo linkups - if no album and title given it will return the artist id, else the id of the matching video
  int GetMatchingMusicVideo(const std::string& strArtist, const std::string& strAlbum = "", const std::string& strTitle = "");

  // searching functions
  void GetMoviesByActor(const std::string& strActor, CFileItemList& items);
  void GetTvShowsByActor(const std::string& strActor, CFileItemList& items);
  void GetEpisodesByActor(const std::string& strActor, CFileItemList& items);

  void GetMusicVideosByArtist(const std::string& strArtist, CFileItemList& items);
  void GetMusicVideosByAlbum(const std::string& strAlbum, CFileItemList& items);

  void GetMovieGenresByName(const std::string& strSearch, CFileItemList& items);
  void GetTvShowGenresByName(const std::string& strSearch, CFileItemList& items);
  void GetMusicVideoGenresByName(const std::string& strSearch, CFileItemList& items);

  void GetMovieCountriesByName(const std::string& strSearch, CFileItemList& items);

  void GetMusicVideoAlbumsByName(const std::string& strSearch, CFileItemList& items);

  void GetMovieActorsByName(const std::string& strSearch, CFileItemList& items);
  void GetTvShowsActorsByName(const std::string& strSearch, CFileItemList& items);
  void GetMusicVideoArtistsByName(const std::string& strSearch, CFileItemList& items);

  void GetMovieDirectorsByName(const std::string& strSearch, CFileItemList& items);
  void GetTvShowsDirectorsByName(const std::string& strSearch, CFileItemList& items);
  void GetMusicVideoDirectorsByName(const std::string& strSearch, CFileItemList& items);

  void GetMoviesByName(const std::string& strSearch, CFileItemList& items);
  void GetTvShowsByName(const std::string& strSearch, CFileItemList& items);
  void GetEpisodesByName(const std::string& strSearch, CFileItemList& items);
  void GetMusicVideosByName(const std::string& strSearch, CFileItemList& items);

  void GetMovieExtrasByName(const std::string& strSearch, CFileItemList& items);

  std::string GetPlotByShowId(int idShow);
  void GetEpisodesByPlot(const std::string& strSearch, CFileItemList& items);
  void GetMoviesByPlot(const std::string& strSearch, CFileItemList& items);

  /*!
   * \brief Link or unlink a movie with a list of shows.
   * \param idMovie Id of the movie
   * \param shows List of show identifiers
   * \param remove true: remove link, false: add link
   * \return true for successful link/unlink of all shows, false on the first error otherwise.
   * \note Attempting to link an already linked show results in failure.
   */
  bool LinkMovieToTvshows(int idMovie, std::vector<int> shows, bool remove);
  bool LinkMovieToTvshow(int idMovie, int idShow, bool bRemove);
  bool IsLinkedToTvshow(int idMovie);
  bool GetLinksToTvShow(int idMovie, std::vector<int>& ids);

  // general browsing
  bool GetGenresNav(const std::string& strBaseDir,
                    CFileItemList& items,
                    VideoDbContentType idContent = VideoDbContentType::UNKNOWN,
                    const Filter& filter = Filter(),
                    bool countOnly = false);
  bool GetCountriesNav(const std::string& strBaseDir,
                       CFileItemList& items,
                       VideoDbContentType idContent = VideoDbContentType::UNKNOWN,
                       const Filter& filter = Filter(),
                       bool countOnly = false);
  bool GetStudiosNav(const std::string& strBaseDir,
                     CFileItemList& items,
                     VideoDbContentType idContent = VideoDbContentType::UNKNOWN,
                     const Filter& filter = Filter(),
                     bool countOnly = false);
  bool GetYearsNav(const std::string& strBaseDir,
                   CFileItemList& items,
                   VideoDbContentType idContent = VideoDbContentType::UNKNOWN,
                   const Filter& filter = Filter());
  bool GetActorsNav(const std::string& strBaseDir,
                    CFileItemList& items,
                    VideoDbContentType idContent = VideoDbContentType::UNKNOWN,
                    const Filter& filter = Filter(),
                    bool countOnly = false);
  bool GetDirectorsNav(const std::string& strBaseDir,
                       CFileItemList& items,
                       VideoDbContentType idContent = VideoDbContentType::UNKNOWN,
                       const Filter& filter = Filter(),
                       bool countOnly = false);
  bool GetWritersNav(const std::string& strBaseDir,
                     CFileItemList& items,
                     VideoDbContentType idContent = VideoDbContentType::UNKNOWN,
                     const Filter& filter = Filter(),
                     bool countOnly = false);
  bool GetSetsNav(const std::string& strBaseDir,
                  CFileItemList& items,
                  VideoDbContentType idContent = VideoDbContentType::UNKNOWN,
                  const Filter& filter = Filter(),
                  bool ignoreSingleMovieSets = false);
  bool GetTagsNav(const std::string& strBaseDir,
                  CFileItemList& items,
                  VideoDbContentType idContent = VideoDbContentType::UNKNOWN,
                  const Filter& filter = Filter(),
                  bool countOnly = false);

  bool GetMusicVideoAlbumsNav(const std::string& strBaseDir, CFileItemList& items, int idArtist, const Filter &filter = Filter(), bool countOnly = false);

  bool GetMoviesNav(const std::string& strBaseDir, CFileItemList& items, int idGenre=-1, int idYear=-1, int idActor=-1, int idDirector=-1, int idStudio=-1, int idCountry=-1, int idSet=-1, int idTag=-1, const SortDescription &sortDescription = SortDescription(), int getDetails = VideoDbDetailsNone);
  bool GetTvShowsNav(const std::string& strBaseDir, CFileItemList& items, int idGenre=-1, int idYear=-1, int idActor=-1, int idDirector=-1, int idStudio=-1, int idTag=-1, const SortDescription &sortDescription = SortDescription(), int getDetails = VideoDbDetailsNone);
  bool GetSeasonsNav(const std::string& strBaseDir, CFileItemList& items, int idActor=-1, int idDirector=-1, int idGenre=-1, int idYear=-1, int idShow=-1, bool getLinkedMovies = true);
  bool GetEpisodesNav(const std::string& strBaseDir, CFileItemList& items, int idGenre=-1, int idYear=-1, int idActor=-1, int idDirector=-1, int idShow=-1, int idSeason=-1, const SortDescription &sortDescription = SortDescription(), int getDetails = VideoDbDetailsNone);
  bool GetMusicVideosNav(const std::string& strBaseDir, CFileItemList& items, int idGenre=-1, int idYear=-1, int idArtist=-1, int idDirector=-1, int idStudio=-1, int idAlbum=-1, int idTag=-1, const SortDescription &sortDescription = SortDescription(), int getDetails = VideoDbDetailsNone);

  bool GetRecentlyAddedMoviesNav(const std::string& strBaseDir, CFileItemList& items, unsigned int limit=0, int getDetails = VideoDbDetailsNone);
  bool GetRecentlyAddedEpisodesNav(const std::string& strBaseDir, CFileItemList& items, unsigned int limit=0, int getDetails = VideoDbDetailsNone);
  bool GetRecentlyAddedMusicVideosNav(const std::string& strBaseDir, CFileItemList& items, unsigned int limit=0, int getDetails = VideoDbDetailsNone);
  bool GetInProgressTvShowsNav(const std::string& strBaseDir,
                               CFileItemList& items,
                               int getDetails = VideoDbDetailsNone);

  bool GetMoviesBySet(const std::string& baseDir, CFileItemList& items, int idSet);

  bool HasContent();
  bool HasContent(VideoDbContentType type);
  bool HasSets() const;

  void CleanDatabase(CGUIDialogProgressBarHandle* handle = nullptr,
                     const std::set<int>& paths = std::set<int>(),
                     bool showProgress = true);

  enum class FileExistsAction
  {
    ACTION_NONE,
    ACTION_UPDATE
  };

  int AddOrUpdateFile(const std::string& fileAndPath,
                      const std::string& parentPath,
                      const FileRecord& fileInfo,
                      FileExistsAction existsAction);

  /*! \brief Add a file to the database, if necessary
   If the file is already in the database, we simply return its id.
   \param url - full path of the file to add.
   \param parentPath the parent path of the path to add. If empty, URIUtils::GetParentPath() will determine the path.
   \param dateAdded datetime when the file was added to the filesystem/database
   \param playcount the playcount of the file to add.
   \param lastPlayed the date and time when the file to add was last played.
   \return id of the file, -1 if it could not be added.
   */
  int AddFile(const std::string& url,
              const std::string& parentPath = "",
              const CDateTime& dateAdded = CDateTime(),
              int playcount = 0,
              const CDateTime& lastPlayed = CDateTime());

  /*! \brief Add a file to the database, if necessary
   Works for both videodb:// items and normal fileitems
   \param item CFileItem to add.
   \return id of the file, -1 if it could not be added.
   */
  int AddFile(const CFileItem& item);

  /*! \brief Add a file to the database, if necessary
   Works for both videodb:// items and normal fileitems
   \param url full path of the file to add.
   \param details details of the item to add.
   \return id of the file, -1 if it could not be added.
   */
  int AddFile(const CVideoInfoTag& details, const std::string& parentPath = "");

  /*! \brief Add a path to the database, if necessary
   If the path is already in the database, we simply return its id.
   \param strPath the path to add
   \param parentPath the parent path of the path to add. If empty, URIUtils::GetParentPath() will determine the path.
   \param dateAdded datetime when the path was added to the filesystem/database
   \return id of the file, -1 if it could not be added.
   */
  int AddPath(const std::string& strPath, const std::string &parentPath = "", const CDateTime& dateAdded = CDateTime());

  /*! \brief Updates the dateAdded field in the files table for the file
   with the given idFile and the given path based on the files modification date
   \param details details of the video file
   */
  void UpdateFileDateAdded(CVideoInfoTag& details);

  void ExportToXML(const std::string &path, bool singleFile = true, bool images=false, bool actorThumbs=false, bool overwrite=false);
  void ExportArt(const CFileItem& item, const KODI::ART::Artwork& artwork, bool overwrite) const;
  void ExportActorThumbs(const std::string& path,
                         const std::string& singlePath,
                         const CVideoInfoTag& tag,
                         bool singleFiles,
                         bool overwrite = false,
                         const std::string& tvshowDir = "") const;
  void ImportFromXML(const std::string &path);
  void DumpToDummyFiles(const std::string &path);
  bool ImportArtFromXML(const TiXmlNode* node, KODI::ART::Artwork& artwork) const;

  // smart playlists and main retrieval work in these functions
  bool GetMoviesByWhere(const std::string& strBaseDir, const Filter &filter, CFileItemList& items, const SortDescription &sortDescription = SortDescription(), int getDetails = VideoDbDetailsNone);
  bool GetSetsByWhere(const std::string& strBaseDir, const Filter &filter, CFileItemList& items, bool ignoreSingleMovieSets = false);
  bool GetTvShowsByWhere(const std::string& strBaseDir, const Filter &filter, CFileItemList& items, const SortDescription &sortDescription = SortDescription(), int getDetails = VideoDbDetailsNone);
  bool GetSeasonsByWhere(const std::string& strBaseDir, const Filter &filter, CFileItemList& items, bool appendFullShowPath = true, const SortDescription &sortDescription = SortDescription());
  bool GetEpisodesByWhere(const std::string& strBaseDir, const Filter &filter, CFileItemList& items, bool appendFullShowPath = true, const SortDescription &sortDescription = SortDescription(), int getDetails = VideoDbDetailsNone);
  bool GetMusicVideosByWhere(const std::string &baseDir, const Filter &filter, CFileItemList& items, bool checkLocks = true, const SortDescription &sortDescription = SortDescription(), int getDetails = VideoDbDetailsNone);

  // retrieve sorted and limited items
  bool GetSortedVideos(const MediaType &mediaType, const std::string& strBaseDir, const SortDescription &sortDescription, CFileItemList& items, const Filter &filter = Filter());

  // retrieve a list of items
  bool GetItems(const std::string &strBaseDir, CFileItemList &items, const Filter &filter = Filter(), const SortDescription &sortDescription = SortDescription());
  bool GetItems(const std::string &strBaseDir, const std::string &mediaType, const std::string &itemType, CFileItemList &items, const Filter &filter = Filter(), const SortDescription &sortDescription = SortDescription());
  bool GetItems(const std::string& strBaseDir,
                VideoDbContentType mediaType,
                const std::string& itemType,
                CFileItemList& items,
                const Filter& filter = Filter(),
                const SortDescription& sortDescription = SortDescription());
  std::string GetItemById(const std::string &itemType, int id);

  // partymode
  /*! \brief Gets music video IDs in random order that match the where clause
  \param strWhere the SQL where clause to apply in the query
  \param songIDs a vector of <2, id> pairs suited to party mode use
  \return count of music video IDs found.
  */
  unsigned int GetRandomMusicVideoIDs(const std::string& strWhere, std::vector<std::pair<int, int> > &songIDs);

  static MediaType VideoContentTypeToString(VideoDbContentType type)
  {
    switch (type)
    {
      case VideoDbContentType::MOVIES:
        return MediaTypeMovie;
      case VideoDbContentType::TVSHOWS:
        return MediaTypeTvShow;
      case VideoDbContentType::EPISODES:
        return MediaTypeEpisode;
      case VideoDbContentType::MUSICVIDEOS:
        return MediaTypeMusicVideo;
      default:
        return {};
    }
  }

  bool SetArtForItem(int mediaId,
                     const MediaType& mediaType,
                     const std::string& artType,
                     const std::string& url);
  bool SetArtForItem(int mediaId, const MediaType& mediaType, const KODI::ART::Artwork& art);
  bool GetArtForItem(int mediaId, const MediaType& mediaType, KODI::ART::Artwork& art);
  std::string GetArtForItem(int mediaId, const MediaType &mediaType, const std::string &artType);

  void UpdateArtForItem(int mediaId, const MediaType& mediaType) const;

  /*!
   * \brief Retrieve all art for the given video asset, with optional fallback to the art of the
   * parent/owner of the asset
   * \param assetId id of the file of the asset
   * \param fallback optionally request fallback to the art of the parent/owner for each art type
     that is not defined for the asset
   * \param art collection of the retrieved art
   * \return 
  */
  bool GetArtForAsset(int assetId, ArtFallbackOptions fallback, KODI::ART::Artwork& art);
  bool HasArtForItem(int mediaId, const MediaType &mediaType);
  bool RemoveArtForItem(int mediaId, const MediaType &mediaType, const std::string &artType);
  bool RemoveArtForItem(int mediaId,
                        const MediaType& mediaType,
                        const std::set<std::string, std::less<>>& artTypes);
  /*!
   * \brief Retrieve season information of a TV show.
   * \param[in] showId ID of the show
   * \param[out] seasons Map of the season information. Key = ID of the season, values = season number
   * \return true for success, false otherwise
   */
  bool GetTvShowSeasons(int showId, std::map<int, int> &seasons);
  /*!
   * \brief Retrieve season information of a TV show.
   * \param[in] showId ID of the show
   * \param[out] seasons Map of the season information. Key = season number, values = SeasonAttributes structure
   * \return true for success, false otherwise
   */
  bool GetTvShowSeasons(int showId, std::map<int, CVideoInfoTag::SeasonAttributes>& seasons);

  /*!
   * \brief Get the custom named season.
   * \param tvshowId The tv show id relative to the season.
   * \param seasonId The season id for which to search the named title.
   * \return The named title if found, otherwise empty.
   */
  std::string GetTvShowNamedSeasonById(int tvshowId, int seasonId) const;

  bool GetTvShowSeasonArt(int mediaId, KODI::ART::SeasonsArtwork& seasonArt);
  bool GetArtTypes(const MediaType &mediaType, std::vector<std::string> &artTypes);

  /*! \brief Fetch the distinct types of available-but-unassigned art held in the
  database for a specific media item.
  \param mediaId the id in the media table.
  \param mediaType the type of media, which corresponds to the table the item resides in.
  \return the types of art e.g. "thumb", "fanart", etc.
  */
  std::vector<std::string> GetAvailableArtTypesForItem(int mediaId, const MediaType& mediaType);

  /*! \brief Fetch the list of available-but-unassigned art URLs held in the
  database for a specific media item and art type.
  \param mediaId the id in the media table.
  \param mediaType corresponds to the table the item resides in.
  \param artType e.g. "thumb", "fanart", etc.
  \return list of URLs
  */
  std::vector<CScraperUrl::SUrlEntry> GetAvailableArtForItem(
    int mediaId, const MediaType& mediaType, const std::string& artType);

  int AddTag(const std::string &tag);
  void AddTagToItem(int idItem, int idTag, const std::string &type);
  void RemoveTagFromItem(int idItem, int idTag, const std::string &type);
  void RemoveTagsFromItem(int idItem, const std::string &type);

  bool GetFilter(CDbUrl &videoUrl, Filter &filter, SortDescription &sorting) override;

  /*! \brief Will check if the season exists and if that is not the case add it to the database.
  \param[in] showID The id of the show in question.
  \param[in] season The season number we want to add.
  \param[in] name Name of the season
  \param[in] plot Plot of the season
  \return The dbId of the season.
  */
  int AddSeason(int showID, int season, const std::string& name = "", const std::string& plot = "");
  int AddSet(const std::string& strSet,
             const std::string& strOverview = "",
             const std::string& strOriginalSet = "",
             const bool updateOverview = true);
  void ClearMovieSet(int idMovie);
  void SetMovieSet(int idMovie, int idSet);
  bool SetVideoUserRating(int dbId, int rating, const MediaType& mediaType);
  bool GetUseAllExternalAudioForVideo(const std::string& videoPath);

  std::string GetSetByNameLike(const std::string& nameLike) const;

  std::string GetVideoItemTitle(VideoDbContentType itemType, int dbId);
  std::string GetVideoVersionById(int id);
  int GetVideoVersionByTitle(const std::string& title) const;
  void GetVideoVersions(VideoDbContentType itemType,
                        int dbId,
                        CFileItemList& items,
                        VideoAssetType videoAssetType);
  void GetDefaultVideoVersion(VideoDbContentType itemType, int dbId, CFileItem& item);

  /*!
   * \brief Remove a video from the library and transfer all of its assets to another video of the
   * same type.
   * \param itemType[in] Type of the video being converted
   * \param dbIdSource[in] id of the video being converted
   * \param dbIdTarget[in] id that the video will be attached to
   * \param idVideoVersion[in] new versiontype of the default version of the video
   *                           special value -1: keep the current versiontype of the video.
   * \param assetType[in] new asset type of the default version of the video.
   * \param cascadeAction[in] action to take on the assets of the video being converted
   *        (used to preserve streamdetails for bluray playlists)
   * \return true for success, false otherwise
   */
  bool ConvertVideoToVersion(VideoDbContentType itemType,
                             int dbIdSource,
                             int dbIdTarget,
                             int idVideoVersion,
                             VideoAssetType assetType,
                             DeleteMovieCascadeAction cascadeAction);

  /*!
   * \brief Adds or updates a version of an existing movie to the database
   * \param itemType type of the video being converted
   * \param dbIdSource id of the video being converted
   * \param idVideoVersion new versiontype of the default version of the video
   * \param assetType new asset type of the default version of the video
   * \return true if success, false otherwise
   */
  bool AddOrUpdateVideoVersion(VideoDbContentType itemType,
                               int dbIdSource,
                               int idFile,
                               int idVideoVersion,
                               VideoAssetType assetType);

  bool SetDefaultVideoVersion(VideoDbContentType itemType, int dbId, int idFile);
  void SetVideoVersion(int idFile, int idVideoVersion);
  int AddOrValidateVideoVersionType(const std::string& typeVideoVersion);
  int AddVideoVersionType(const std::string& typeVideoVersion,
                          VideoAssetTypeOwner owner,
                          VideoAssetType assetType);
  /*!
   * \brief Create a new video asset from the provided item and type and attach it to an owner
   * A file record is created for items with a path new to the database.
   * \param[in] itemType Parent's type
   * \param[in] dbId  Parent's id
   * \param[in] idVideoAsset Video asset identifier / name
   * \param[in] videoAssetType Type of the video asset
   * \param[in] item Item to be made into a video asset
   * \return Success status. true:success, false:failure
   */
  bool AddVideoAsset(VideoDbContentType itemType,
                     int dbId,
                     int idVideoAsset,
                     VideoAssetType videoAssetType,
                     CFileItem& item);
  bool DeleteVideoAsset(int idFile);
  bool IsDefaultVideoVersion(int idFile);
  bool GetVideoVersionTypes(VideoDbContentType idContent,
                            VideoAssetType assetType,
                            CFileItemList& items);

  /*!
   * \brief Check the validity of the video asset type id.
   * \param[in] typeId Id of the video asset type
   * \param[in] idContent db item type
   * \param[in] asset type of the video asset type
   * \return true when the id exists and matches the provided content and asset type, false otherwise.
   */
  bool IsValidVideoAssetType(int typeId, VideoDbContentType idContent, VideoAssetType asset);
  bool SetVideoVersionDefaultArt(int dbId, int idFrom, const MediaType& mediaType);
  void UpdateVideoVersionTypeTable();
  bool GetVideoVersionsNav(const std::string& strBaseDir,
                           CFileItemList& items,
                           VideoDbContentType idContent = VideoDbContentType::UNKNOWN,
                           const Filter& filter = Filter());
  VideoAssetInfo GetVideoVersionInfo(const std::string& filenameAndPath);
  bool UpdateAssetsOwner(const std::string& mediaType, int dbIdSource, int dbIdTarget);

  int GetMovieId(const std::string& strFilenameAndPath);
  std::string GetMovieTitle(int idMovie);

  enum MatchingMask : uint8_t
  {
    None = 0x00,
    UniqueId = 0x01,
    Path = 0x02,
    Title = 0x04
  };

  void GetSameVideoItems(const CFileItem& item,
                         CFileItemList& items,
                         int matchingMask = UniqueId | Title);
  int GetFileIdByMovie(int idMovie);
  std::string GetFileBasePathById(int idFile);

  /*!
   * @brief Check the passed in list of images if used in this database. Used to clean the image cache.
   * @param imagesToCheck
   * @return a list of the passed in images used by this database.
   */
  std::vector<std::string> GetUsedImages(const std::vector<std::string>& imagesToCheck);

  /*! \brief Find a playlist path for a removable bluray disc.
   \param originalPath A path in the format of bluray://removable://<title_ID>/BDMV/index.bdmv
   \return A path in the format of bluray://removable://<title_ID>/BDMV/PLAYLIST/00000.mpls if found, otherwise an empty string.
   */
  std::string GetRemovableBlurayPath(std::string originalPath);

protected:
  int AddNewMovie(CVideoInfoTag& details);
  int AddNewMusicVideo(CVideoInfoTag& details);

  int GetMusicVideoId(const std::string& strFilenameAndPath);

  /*! \brief Get the id of this fileitem
   Works for both videodb:// items and normal fileitems
   \param item CFileItem to grab the fileid of
   \return id of the file, -1 if it is not in the db.
   */
  int GetFileId(const CFileItem &item);
  int GetFileId(const CVideoInfoTag& details);

  /*! \brief Get the id of the file of this item and store it in the item
   \param details CVideoInfoTag for which to get and store the id of the file
   \return id of the file, -1 if it is not in the db.
   */
  int GetAndFillFileId(CVideoInfoTag& details);

  /*! \brief Get the id of a file from path
   \param url full path to the file
   \return id of the file, -1 if it is not in the db.
   */
  int GetFileId(const std::string& url);

  /*! \brief Get the id of a stack of discs, however its parts are expressed.
   A stack holding a bluray folders or disc images may be stored resolved to the
   bluray:// playlist of each part, whereas the scraper will be looking for a
   stack:// of the base paths.
   \param stackPath a stack:// path of which at least one part is a disc
   \return id of the file, -1 if it is not in the db or several stacks match.
   */
  int GetDiscStackFileId(const std::string& stackPath);

  int AddToTable(const std::string& table, const std::string& firstField, const std::string& secondField, const std::string& value);
  int UpdateRatings(int mediaId, const char *mediaType, const RatingMap& values, const std::string& defaultRating);
  int AddRatings(int mediaId,
                 const char* mediaType,
                 const RatingMap& values,
                 std::string_view defaultRating);
  int UpdateUniqueIDs(int mediaId, const char *mediaType, const CVideoInfoTag& details);
  int AddUniqueIDs(int mediaId, const char *mediaType, const CVideoInfoTag& details);
  int AddActor(const std::string& strActor, const std::string& thumbURL, const std::string &thumb = "");

  int AddTvShow();

  /*! \brief Adds a path to the tvshow link table.
   \param idShow the id of the show.
   \param path the path to add.
   \param dateAdded date/time when the path was added
   \return true if successfully added, false otherwise.
   */
  bool AddPathToTvShow(int idShow, const std::string& path, const CDateTime& dateAdded);

  /*! \brief Check whether a show is already in the library.
   Matches on unique identifier or matching title and premiered date.
   \param show the details of the show to check for.
   \return the show id if found, else -1.
   */
  int GetMatchingTvShow(const CVideoInfoTag& show) const;

  // link functions - these two do all the work
  void AddLinkToActor(int mediaId, const char *mediaType, int actorId, const std::string &role, int order);
  void AddToLinkTable(int mediaId,
                      const std::string& mediaType,
                      const std::string& table,
                      int valueId,
                      const char* foreignKey = nullptr);
  void RemoveFromLinkTable(int mediaId,
                           const std::string& mediaType,
                           const std::string& table,
                           int valueId,
                           const char* foreignKey = nullptr);

  void AddLinksToItem(int mediaId, const std::string& mediaType, const std::string& field, const std::vector<std::string>& values);
  void UpdateLinksToItem(int mediaId, const std::string& mediaType, const std::string& field, const std::vector<std::string>& values);
  void AddActorLinksToItem(int mediaId, const std::string& mediaType, const std::string& field, const std::vector<std::string>& values);
  void UpdateActorLinksToItem(int mediaId, const std::string& mediaType, const std::string& field, const std::vector<std::string>& values);

  void AddCast(int mediaId, const char *mediaType, const std::vector<SActorInfo> &cast);

  CVideoInfoTag GetDetailsForMovie(dbiplus::Dataset& pDS, int getDetails = VideoDbDetailsNone);
  CVideoInfoTag GetDetailsForMovie(const dbiplus::sql_record* const record, int getDetails = VideoDbDetailsNone);
  CSetInfoTag GetDetailsForSet(dbiplus::Dataset& pDS) const;
  CSetInfoTag GetDetailsForSet(const dbiplus::sql_record* const record) const;
  CVideoInfoTag GetDetailsForTvShow(dbiplus::Dataset& pDS,
                                    int getDetails = VideoDbDetailsNone,
                                    CFileItem* item = nullptr);
  CVideoInfoTag GetDetailsForTvShow(const dbiplus::sql_record* const record,
                                    int getDetails = VideoDbDetailsNone,
                                    CFileItem* item = nullptr);
  CVideoInfoTag GetBasicDetailsForEpisode(dbiplus::Dataset& pDS) const;
  CVideoInfoTag GetBasicDetailsForEpisode(const dbiplus::sql_record* const record) const;
  CVideoInfoTag GetDetailsForEpisode(dbiplus::Dataset& pDS, int getDetails = VideoDbDetailsNone);
  CVideoInfoTag GetDetailsForEpisode(const dbiplus::sql_record* const record, int getDetails = VideoDbDetailsNone);
  CVideoInfoTag GetDetailsForMusicVideo(dbiplus::Dataset& pDS, int getDetails = VideoDbDetailsNone);
  CVideoInfoTag GetDetailsForMusicVideo(const dbiplus::sql_record* const record, int getDetails = VideoDbDetailsNone);
  bool GetPeopleNav(const std::string& strBaseDir,
                    CFileItemList& items,
                    const char* type,
                    VideoDbContentType idContent = VideoDbContentType::UNKNOWN,
                    const Filter& filter = Filter(),
                    bool countOnly = false);
  bool GetNavCommon(const std::string& strBaseDir,
                    CFileItemList& items,
                    const char* type,
                    VideoDbContentType idContent = VideoDbContentType::UNKNOWN,
                    const Filter& filter = Filter(),
                    bool countOnly = false);
  void GetCast(int media_id, const std::string &media_type, std::vector<SActorInfo> &cast);
  void GetTags(int media_id, const std::string &media_type, std::vector<std::string> &tags);
  void GetRatings(int media_id, const std::string &media_type, RatingMap &ratings);
  void GetUniqueIDs(int media_id, const std::string &media_type, CVideoInfoTag& details);

  template<typename T>
  void GetDetailsFromDB(const dbiplus::sql_record* const record,
                        int min,
                        int max,
                        const T& offsets,
                        CVideoInfoTag& details,
                        int idxOffset = 2) const;
  template<typename T>
  void GetDetailsFromDB(const dbiplus::sql_record* const record,
                        int min,
                        int max,
                        const T& offsets,
                        CSetInfoTag& details,
                        int idxOffset) const;

  template<typename T>
  std::string GetValueString(const CVideoInfoTag& details,
                             int min,
                             int max,
                             const T& offsets) const;

  int SetFileForEpisode(const std::string& fileAndPath,
                        int idEpisode,
                        int oldIdFile,
                        int newIdFile);
  int SetFileForMovie(const std::string& fileAndPath, int idMovie, int oldIdFile, int newIdFile);
  int SetFileForUnknown(const std::string& fileAndPath, int oldIdFile, int newIdFile);

private:
  void CreateTables() override;
  void CreateAnalytics() override;
  void UpdateTables(int version) override;

  /*! \brief Helper to get a database id given a query.
   Returns an integer, -1 if not found, and greater than 0 if found.
   \param query the SQL that will retrieve a database id.
   \return -1 if not found, else a valid database id (i.e. > 0)
   */
  int GetDbId(const std::string& query) const;

  /*! \brief Run a query on the main dataset and return the number of rows
   If no rows are found we close the dataset and return 0.
   \param sql the sql query to run
   \return the number of rows, -1 for an error.
   */
  int RunQuery(const std::string &sql);

  void AppendIdLinkFilter(const char* field,
                          const char* table,
                          const MediaType& mediaType,
                          const char* view,
                          const char* viewKey,
                          const CUrlOptions::UrlOptions& options,
                          Filter& filter) const;
  void AppendLinkFilter(const char* field,
                        const char* table,
                        const MediaType& mediaType,
                        const char* view,
                        const char* viewKey,
                        const CUrlOptions::UrlOptions& options,
                        Filter& filter) const;

  /*! \brief Determine whether the path is using lookup using folders
   \param path the path to check
   \param shows whether this path is from a tvshow (defaults to false)
   */
  bool LookupByFolders(const std::string &path, bool shows = false);

  /*! \brief Get the playcount for a file id
   \param iFileId file id to get the playcount for
   \return the playcount of the item, or -1 on error
   \sa SetPlayCount, IncrementPlayCount, GetPlayCounts
   */
  int GetPlayCount(int iFileId);

  /*! \brief Get the last played time of a filename and path
   \param iFileId file id to get the playcount for
   \return the last played time of the item, or an invalid CDateTime on error
   \sa UpdateLastPlayed
   */
  CDateTime GetLastPlayed(int iFileId);

  bool GetSeasonInfo(int idSeason, CVideoInfoTag& details, bool allDetails, CFileItem* item);

  int GetMinSchemaVersion() const override { return 75; }
  int GetSchemaVersion() const override;
  virtual int GetExportVersion() const { return 1; }
  const char* GetBaseDBName() const override { return "MyVideos"; }

  void ConstructPath(std::string& strDest,
                     const std::string& strPath,
                     const std::string& strFileName) const;
  void SplitPath(const std::string& strFileNameAndPath,
                 std::string& strPath,
                 std::string& strFileName) const;
  void InvalidatePathHash(const std::string& strPath);

  /*! \brief Clear the hash of a path, without adding it to the path table if unknown
   \param strPath the path to clear the hash of
   */
  void ClearPathHash(const std::string& strPath);

  /*! \brief Get a safe filename from a given string
   \param dir directory to use for the file
   \param name movie, show name, or actor to get a safe filename for
   \return safe filename based on this title
   */
  std::string GetSafeFile(const std::string &dir, const std::string &name) const;

  std::vector<int> CleanMediaType(const std::string &mediaType, const std::string &cleanableFileIDs,
                                  std::map<int, bool> &pathsDeleteDecisions, std::string &deletedFileIDs, bool silent);

  static void AnnounceRemove(const std::string& content, int id, bool scanning = false);
  static void AnnounceUpdate(const std::string& content, int id);

  static CDateTime GetDateAdded(const std::string& filename, CDateTime dateAdded = CDateTime());
};
