using System;
using System.IO;
using MediaBrowser.Common.Configuration;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace Jellyfin.Server.Implementations
{
    /// 
    /// Factory class for generating new  instances.
    /// 
    public class JellyfinDbProvider
    {
        private readonly IServiceProvider _serviceProvider;
        private readonly IApplicationPaths _appPaths;
        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// The application's service provider.
        /// The application paths.
        public JellyfinDbProvider(IServiceProvider serviceProvider, IApplicationPaths appPaths)
        {
            _serviceProvider = serviceProvider;
            _appPaths = appPaths;
            using var jellyfinDb = CreateContext();
            jellyfinDb.Database.Migrate();
        }
        /// 
        /// Creates a new  context.
        /// 
        /// The newly created context.
        public JellyfinDb CreateContext()
        {
            var contextOptions = new DbContextOptionsBuilder().UseSqlite($"Filename={Path.Combine(_appPaths.DataPath, "jellyfin.db")}");
            return ActivatorUtilities.CreateInstance(_serviceProvider, contextOptions.Options);
        }
    }
}