using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Database.Configuration;

namespace Database.Services
{
    public interface IDatabaseMigrator
    {
        Task MigrateAsync();
        Task<bool> CanConnectAsync();
        Task<List<string>> GetPendingMigrationsAsync();
        Task<List<string>> GetAppliedMigrationsAsync();
    }
    
    public class DatabaseMigrator : IDatabaseMigrator
    {
        private readonly DatabaseContext _context;
        private readonly DatabaseConfiguration _config;
        private readonly ILogger<DatabaseMigrator> _logger;
        
        public DatabaseMigrator(DatabaseContext context, DatabaseConfiguration config, ILogger<DatabaseMigrator> logger)
        {
            _context = context;
            _config = config;
            _logger = logger;
        }
        
        public async Task MigrateAsync()
        {
            try
            {
                _logger.LogInformation("Starting database migration...");
                
                var pendingMigrations = await _context.Database.GetPendingMigrationsAsync();
                
                if (pendingMigrations.Any())
                {
                    _logger.LogInformation("Found {Count} pending migrations: {Migrations}", 
                        pendingMigrations.Count(), string.Join(", ", pendingMigrations));
                    
                    await _context.Database.MigrateAsync();
                    
                    _logger.LogInformation("Database migration completed successfully");
                }
                else
                {
                    _logger.LogInformation("No pending migrations found");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Database migration failed");
                throw;
            }
        }
        
        public async Task<bool> CanConnectAsync()
        {
            try
            {
                return await _context.Database.CanConnectAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Unable to connect to database");
                return false;
            }
        }
        
        public async Task<List<string>> GetPendingMigrationsAsync()
        {
            try
            {
                var pending = await _context.Database.GetPendingMigrationsAsync();
                return pending.ToList();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error getting pending migrations");
                return new List<string>();
            }
        }
        
        public async Task<List<string>> GetAppliedMigrationsAsync()
        {
            try
            {
                var applied = await _context.Database.GetAppliedMigrationsAsync();
                return applied.ToList();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error getting applied migrations");
                return new List<string>();
            }
        }
    }
}