using CouchLog.Data;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;

namespace CouchLog
{
    public class OnStartUp(ApplicationDbContext CouchLogDB, RoleManager<IdentityRole> RoleManager)
    {
        private readonly ApplicationDbContext CouchLogDB = CouchLogDB;
        private readonly RoleManager<IdentityRole> RoleManager = RoleManager;

        public async Task CreateRoles()
        {
            string[] roleNames = ["Admin", "User"];
            IdentityResult roleResult;

            foreach (var roleName in roleNames)
            {
                var roleExist = await RoleManager.RoleExistsAsync(roleName);
                if (!roleExist)
                {
                    roleResult = await RoleManager.CreateAsync(new IdentityRole(roleName));
                }
            }
        }

        public async Task CreateStanardSettings()
        {
            if(!CouchLogDB.AccountsSettings.Any())
            {
                AccountsSettings accountsSettings = new()
                {
                    Id = 1,
                    IsRegistrationAllowed = true,
                };

                await CouchLogDB.AccountsSettings.AddAsync(accountsSettings);

                await CouchLogDB.SaveChangesAsync();
            }
        }

        public async Task AddBasicDatabaseEntries()
        {
            //##################
            //### MediaTypes ###
            //##################
            List<MediaType> MediaTypes =
            [
                //Video based
                new() { Name="Movie" },
                new() { Name="Series" },
                new() { Name="Anime" },
                new() { Name="Documentary" },
                new() { Name="Short Film" },
                //new() { Name="Music Video" },
                //new() { Name="Live Performance" },

                //Audio based
                new() { Name="Podcast" },
                new() { Name="Audiobook" },
                new() { Name="Music Album" },

                //Interactive
                new() { Name="Video Game" },

                //Text based
                new() { Name = "Book" },
                new() { Name = "Comic" },
            ];

            foreach (MediaType MediaType in MediaTypes)
            {
                if(!await CouchLogDB.MediaType.AnyAsync(m => m.Name == MediaType.Name))
                {
                    await CouchLogDB.MediaType.AddAsync(MediaType);
                }
            }

            //##############
            //### Genres ###
            //##############
            List<Genre> Genres =
            [
                new() { Name = "Action",            CreationTime = DateTime.Now },
                new() { Name = "Animation",         CreationTime = DateTime.Now },
                new() { Name = "Adventure",         CreationTime = DateTime.Now },
                new() { Name = "Comedy",            CreationTime = DateTime.Now },
                new() { Name = "Crime",             CreationTime = DateTime.Now },
                new() { Name = "Drama",             CreationTime = DateTime.Now },
                new() { Name = "Fantasy",           CreationTime = DateTime.Now },
                new() { Name = "History",           CreationTime = DateTime.Now },
                new() { Name = "Horror",            CreationTime = DateTime.Now },
                new() { Name = "Musical",           CreationTime = DateTime.Now },
                new() { Name = "Miniseries",        CreationTime = DateTime.Now },
                new() { Name = "Mystery",           CreationTime = DateTime.Now },
                new() { Name = "Romance",           CreationTime = DateTime.Now },
                new() { Name = "Science Fiction",   CreationTime = DateTime.Now },
                new() { Name = "Thriller",          CreationTime = DateTime.Now },
                new() { Name = "War",               CreationTime = DateTime.Now },
                new() { Name = "Western",           CreationTime = DateTime.Now },
            ];
            
            foreach(Genre Genre in Genres)
            {
                if(! await CouchLogDB.Genres.AnyAsync(m => m.Name == Genre.Name))
                {
                    await CouchLogDB.Genres.AddAsync(Genre);
                }
            }

            
            //##########################
            //### StreamingPlatforms ###
            //##########################
            List<StreamingPlatform> StreamingPlatforms =
            [
                new() { Name = "Netflix", PicturePath="StreamingPlatforms/Netflix.png", CreationTime = DateTime.Now },
                new() { Name = "Prime Video", PicturePath="StreamingPlatforms/Prime-Video.png", CreationTime = DateTime.Now },
                new() { Name = "Disney+", PicturePath="StreamingPlatforms/Disney+.png", CreationTime = DateTime.Now },
                new() { Name = "Apple TV", PicturePath="StreamingPlatforms/AppleTV.png", CreationTime = DateTime.Now },
                new() { Name = "WOW TV", PicturePath="StreamingPlatforms/WOWTV.png", CreationTime = DateTime.Now },
                new() { Name = "Paramount+", PicturePath="StreamingPlatforms/Paramount+.png", CreationTime = DateTime.Now },
                new() { Name = "Joyn", PicturePath="StreamingPlatforms/Joyn.png", CreationTime = DateTime.Now },
            ];

            foreach(StreamingPlatform StreamingPlatform in StreamingPlatforms)
            {
                if(! await CouchLogDB.StreamingPlatforms.AnyAsync(m => m.Name == StreamingPlatform.Name))
                {
                    await CouchLogDB.StreamingPlatforms.AddAsync(StreamingPlatform);
                }
            }

            //##########################
            //###### WatchStates #######
            //##########################
            List<UserWatchStatus> UserWatchStatuses =
            [
                new() { Name = "Not watched", CreationTime = DateTime.Now },
                new() { Name = "Watching", CreationTime = DateTime.Now },
                new() { Name = "Finished", CreationTime = DateTime.Now },
                new() { Name = "Waiting for next Season", CreationTime = DateTime.Now },
                new() { Name = "Aborted", CreationTime= DateTime.Now },
            ];

            foreach(UserWatchStatus UserWatchStatus in UserWatchStatuses)
            {
                if(! await CouchLogDB.UserWatchStatuses.AnyAsync(m => m.Name == UserWatchStatus.Name))
                {
                    await CouchLogDB.UserWatchStatuses.AddAsync(UserWatchStatus);
                }
            }

            //#################################################
            //###### Old-Things that need to be deleted #######
            //#################################################
            List<UserWatchStatus> oldUserWatchStatuses =
            [
                new() { Name = "Started" , CreationTime = DateTime.Now },
            ];

            foreach(UserWatchStatus oldUserWatchStatus in oldUserWatchStatuses)
            {
                UserWatchStatus? toDeletedUserWatchStatus = await CouchLogDB.UserWatchStatuses.FirstOrDefaultAsync(m => m.Name == oldUserWatchStatus.Name);

                if(toDeletedUserWatchStatus != null)
                {
                    CouchLogDB.UserWatchStatuses.Remove(toDeletedUserWatchStatus);
                }
            }

            await CouchLogDB.SaveChangesAsync();
        }
    }
}
