using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Sockeye.Models;
using Sockeye.Util;
using System.IO;
using System.Runtime.InteropServices;

namespace Sockeye.Biz
{


    /// <summary>
    /// Backup 
    /// 
    /// </summary>
    internal static class CoreJobBackup
    {
        private static ILogger log = Sockeye.Util.ApplicationLogging.CreateLogger("CoreJobBackup");
        private static bool BackupIsRunning = false;
        private const int MAXIMUM_MS_ALLOWED_FOR_PROCESSING = 10 * 60 * 1000;//wild assed guess 10 minutes maximum to run backup command, ever

        ////////////////////////////////////////////////////////////////////////////////////////////////
        // BACK-THE-FUCK-UP
        //
        public static async Task DoWorkAsync(bool OnDemand = false)
        {
            if (BackupIsRunning) return;
            if (!OnDemand)
            {
                log.LogTrace("Checking if backup should run");
                if (!ServerGlobalOpsSettingsCache.Backup.Active)
                {
                    log.LogDebug("Automatic backup is set to INACTIVE - not backing up");
                    return;
                }

                if (DateTime.UtcNow < ServerGlobalOpsSettingsCache.NextBackup)
                {
                    log.LogTrace("Not past backup time yet");
                    return;
                }
            }
            Sockeye.Api.ControllerHelpers.ApiServerState apiServerState = null;

            try
            {
                BackupIsRunning = true;

                //LOCK DOWN SERVER
                apiServerState = (Sockeye.Api.ControllerHelpers.ApiServerState)ServiceProviderProvider.Provider.GetService(typeof(Sockeye.Api.ControllerHelpers.ApiServerState));
                apiServerState.SetClosed("BACKUP RUNNING");
                var jobstartmessage = $"LT:Backup LT:StartJob {(OnDemand ? "manual / on demand" : "scheduled")} ";
                await JobsBiz.LogJobAsync(Guid.Empty, jobstartmessage);

                DateTime dtStartBackup = DateTime.Now;
                log.LogDebug("Backup starting");
                var DemandFileNamePrepend = OnDemand ? "manual-" : string.Empty;
                //*************
                //DO DATA BACKUP
                //build command
                //this is valid on windows
                //C:\data\code\PostgreSQLPortable_12.0\App\PgSQL\bin\pg_dump --dbname=postgresql://postgres:raven@127.0.0.1:5432/Sockeye -Fc > huge_new.backup

                // await JobsBiz.LogJobAsync(Guid.Empty, $"Data backup starting");
                Npgsql.NpgsqlConnectionStringBuilder PostgresConnectionString = new Npgsql.NpgsqlConnectionStringBuilder(ServerBootConfig.SOCKEYE_DB_CONNECTION);
                var DBNameParameter = $"--dbname=postgresql://{PostgresConnectionString.Username}:{PostgresConnectionString.Password}@{PostgresConnectionString.Host}:{PostgresConnectionString.Port}/{PostgresConnectionString.Database}";

                var DataBackupFile = $"{DemandFileNamePrepend}db-{FileUtil.GetSafeDateFileName()}.backup";//presentation issue so don't use UTC for this one
                DataBackupFile = FileUtil.GetFullPathForBackupFile(DataBackupFile);

                var BackupUtilityCommand = "pg_dump";


                if (!string.IsNullOrWhiteSpace(ServerBootConfig.SOCKEYE_BACKUP_PG_DUMP_PATH))
                    BackupUtilityCommand = Path.Combine(ServerBootConfig.SOCKEYE_BACKUP_PG_DUMP_PATH, BackupUtilityCommand);

                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    //put quotes around paths if spaces in them
                    if (BackupUtilityCommand.Contains(' '))
                    {
                        BackupUtilityCommand = $"\"{BackupUtilityCommand}\"";
                    }

                    if (DataBackupFile.Contains(' '))
                    {
                        DataBackupFile = $"\"{DataBackupFile}\"";
                    }
                }

                var Arguments = $"{DBNameParameter} -Fc > {DataBackupFile}";

                var Result = RunProgram.Run(BackupUtilityCommand, Arguments, log, MAXIMUM_MS_ALLOWED_FOR_PROCESSING);
                if (!string.IsNullOrWhiteSpace(Result))
                {
                    var msg = $"Error during data backup \"{Result}\"";
                    await JobsBiz.LogJobAsync(Guid.Empty, msg);
                    log.LogError($"BACKUP ERROR: {Result}");
                    await NotifyEventHelper.AddGeneralNotifyEvent(NotifyEventType.BackupStatus, msg, "Backup");
                }
                else
                {
                    log.LogDebug("Backup of database completed OK");

                    //DO FILE BACKUP IF ATTACHMENTS BACKED UP
                    if (ServerGlobalOpsSettingsCache.Backup.BackupAttachments)
                    {
                        await JobsBiz.LogJobAsync(Guid.Empty, $"LT:Backup LT:Attachments");
                        FileUtil.BackupAttachments(DemandFileNamePrepend);
                        log.LogDebug("Backup of file attachments completed OK");
                    }

                    //PRUNE DATA BACKUP SETS NOT KEPT
                    await JobsBiz.LogJobAsync(Guid.Empty, $"LT:BackupDeleteOld");

     
                    FileUtil.DatabaseBackupCleanUp(ServerGlobalOpsSettingsCache.Backup.BackupSetsToKeep);



                    //v.next - COPY TO ONLINE STORAGE
                    //***************     

                    log.LogDebug("Backup completed");
                    var duration = DateTime.Now - dtStartBackup;
                    await NotifyEventHelper.AddGeneralNotifyEvent(NotifyEventType.BackupStatus, $"Backup ({(OnDemand ? "manual / on demand" : "scheduled")}) completed successfully, duration (h:m:s.ms): {duration.ToString()}, server re-opened", "Backup");
                }
            }
            catch (Exception ex)
            {
                await JobsBiz.LogJobAsync(Guid.Empty, "LT:JobFailed");
                await JobsBiz.LogJobAsync(Guid.Empty, ExceptionUtil.ExtractAllExceptionMessages(ex));
                log.LogError(ex, "Backup failed");
                await NotifyEventHelper.AddGeneralNotifyEvent(NotifyEventType.BackupStatus, "Backup failed", "Backup", ex);
                throw;
            }
            finally
            {
                //bump the backup date if automatic backup
                if (!OnDemand)
                    ServerGlobalOpsSettingsCache.SetNextBackup();
                apiServerState.ResumePriorState();
                BackupIsRunning = false;
                await JobsBiz.LogJobAsync(Guid.Empty, "LT:JobCompleted");

            }
        }



        /////////////////////////////////////////////////////////////////////
    }//eoc
}//eons

