using Dapper;
using NzbDrone.Core.Datastore;

namespace NzbDrone.Core.Housekeeping.Housekeepers
{
    public class CleanupOrphanedSubtitleFiles : IHousekeepingTask
    {
        private readonly IMainDatabase _database;

        public CleanupOrphanedSubtitleFiles(IMainDatabase database)
        {
            _database = database;
        }

        public void Clean()
        {
            DeleteOrphanedByMovie();
            DeleteOrphanedByMovieFile();
        }

        private void DeleteOrphanedByMovie()
        {
            using (var mapper = _database.OpenConnection())
            {
                mapper.Execute(@"DELETE FROM SubtitleFiles
                                     WHERE Id IN (
                                     SELECT SubtitleFiles.Id FROM SubtitleFiles
                                     LEFT OUTER JOIN Movies
                                     ON SubtitleFiles.MovieId = Movies.Id
                                     WHERE Movies.Id IS NULL)");
            }
        }

        private void DeleteOrphanedByMovieFile()
        {
            using (var mapper = _database.OpenConnection())
            {
                mapper.Execute(@"DELETE FROM SubtitleFiles
                                     WHERE Id IN (
                                     SELECT SubtitleFiles.Id FROM SubtitleFiles
                                     LEFT OUTER JOIN MovieFiles
                                     ON SubtitleFiles.MovieFileId = MovieFiles.Id
                                     WHERE SubtitleFiles.MovieFileId > 0
                                     AND MovieFiles.Id IS NULL)");
            }
        }
    }
}
