using Femto.Common.Domain;
using Femto.Modules.Auth.Contracts;
using Femto.Modules.Auth.Models.Events;

namespace Femto.Modules.Auth.Models;

internal class UserIdentity : Entity
{
    public Guid Id { get; private set; }

    public string Username { get; private set; }

    public Password? Password { get; private set; }

    public ICollection<UserRole> Roles { get; private set; } = [];

    private UserIdentity() { }

    public UserIdentity(string username)
    {
        this.Id = Guid.CreateVersion7();
        this.Username = username;

        this.Roles = [new UserRole(Role.User)];

        this.AddDomainEvent(new UserWasCreatedEvent(this));
    }

    public void SetPassword(string password)
    {
        if (this.Password is not null)
            this.AddDomainEvent(new UserPasswordChangedDomainEvent(this));
        this.Password = new Password(password);
    }

    public bool HasPassword(string requestPassword)
    {
        if (this.Password is null)
        {
            return false;
        }

        return this.Password.Check(requestPassword);
    }
}

public class SetPasswordError(string message, Exception inner) : DomainError(message, inner);
