using System.Text.Json;

namespace ConfigurationPannel.Services;

public class UserService
{
    private readonly string _usersFilePath;

    public UserService(IWebHostEnvironment env)
    {
        _usersFilePath = Path.Combine(env.ContentRootPath, "users.json");
    }

    public async Task<bool> ValidateCredentialsAsync(string username, string password)
    {
        if (!File.Exists(_usersFilePath))
            return false;

        var json = await File.ReadAllTextAsync(_usersFilePath);
        var userStore = JsonSerializer.Deserialize<UserStore>(json, new JsonSerializerOptions(){PropertyNameCaseInsensitive = true});
        var user = userStore?.Users.FirstOrDefault(u => u.Username == username);

        if (user == null)
            return false;

        return user.Password==password;
    }
}

public class UserStore
{
    public List<User> Users { get; set; } = new();
}

public class User
{
    public string Username { get; set; } = string.Empty;
    public string Password { get; set; } = string.Empty;
}
