using WebVentaCoche.DataBase;
using WebVentaCoche.Helpers;
using WebVentaCoche.Enums;
using WebVentaCoche.Models;

namespace WebVentaCoche.Services
{
    public class SeedDb
    {
        private readonly ApplicationDbContext _context;
        private readonly IUserHelper _userHelper;

        public SeedDb(ApplicationDbContext context, IUserHelper userHelper)
        {
            _context = context;
            _userHelper = userHelper;
        }

        public async Task SeedAsync()
        {
            await _context.Database.EnsureCreatedAsync();
            await ValidateRolesAsync();
            await ValidateUserAsync("Raul", "Gonzalez", "rgonsal93@gmail.com", "666765457", UserType.Administrador);
        }
        private async Task<User> ValidateUserAsync(string name, string surname, string email, string phone, UserType userType)
        {
            var user = await _userHelper.GetUserAsync(email);
            if (user == null)
            {
                user = new User
                {
                    Name = name,
                    Surname = surname,
                    Email = email,
                    UserName = email,
                    PhoneNumber = phone,
                    UserType = userType
                };
                await _userHelper.AddUserAsync(user, "123456");
                await _userHelper.AddUserRoleAsync(user, userType.ToString());
            }

            return user;
        }
        private async Task ValidateRolesAsync()
        {
            await _userHelper.ValidateRoleAsync(UserType.Administrador.ToString());
            await _userHelper.ValidateRoleAsync(UserType.Usuario.ToString());
        }
    }
}
