using Microsoft.EntityFrameworkCore;
using qdr.app.bundleboiler.business.Services.Interfaces;
using qdr.app.bundleboiler.data.Model;
using Quadarax.Foundation.Core.Attributes;
using Quadarax.Foundation.Core.Data;
using Quadarax.Foundation.Core.Data.Interface.Domain;
using Quadarax.Foundation.Core.Logging;

namespace qdr.app.bundleboiler.business.Services
{
    [DiModule]
    [DiImplementsOf(typeof(IDatabaseService), LifeCycleTypeEnum.Scoped)]
    public class DatabaseService : BaseService, IDatabaseService
    {
        private readonly IDomain _dbcontext;

        public DatabaseService(BoilerDbContext dbcontext, IOptions options, ILogger logger) : base(options, logger)
        {
            _dbcontext = dbcontext ?? throw new ArgumentNullException(nameof(dbcontext));
        }

        public IResult EnsureDatabase()
        {
            return Operation(() =>
            {
                if (((DbContext)_dbcontext).Database.EnsureCreated())
                {
                    Log.Log(LogSeverityEnum.Info, $"Database @ '{Options.ConnectionString}' created");
                }
                return Result.Ok;
            });

        }

        public IResult EnsureEnumerations()
        {
            return Operation(() =>
            {
                if (((DbContext)_dbcontext).Database.EnsureCreated())
                {
                    Log.Log(LogSeverityEnum.Info, $"Database @ '{Options.ConnectionString}' created");
                }
                return Result.Ok;
            });
        }
    }
}
