using NexusShell.Models;

namespace NexusShell.Services.Identity;

public sealed class NexusIdentityService
{
    private readonly NexusOrganizationService _organizations;
    private readonly NexusUserProfileService _users;
    private readonly NexusEnrollmentService _enrollment;
    private readonly NexusDeviceTrustService _trust;

    public NexusIdentityService(
        NexusOrganizationService organizations,
        NexusUserProfileService users,
        NexusEnrollmentService enrollment,
        NexusDeviceTrustService trust)
    {
        _organizations = organizations;
        _users = users;
        _enrollment = enrollment;
        _trust = trust;
    }

    public NexusOrganizationService Organizations => _organizations;
    public NexusUserProfileService Users => _users;
    public NexusEnrollmentService Enrollment => _enrollment;
    public NexusDeviceTrustService Trust => _trust;

    public bool IsEnrolled => _enrollment.IsEnrolled;

    public NexusIdentitySnapshot GetSnapshot()
    {
        var device = _enrollment.CurrentDevice;
        if (device is not null)
        {
            _trust.TouchLastSeen(device);
        }

        return new NexusIdentitySnapshot
        {
            Organization = _organizations.Current,
            User = _users.Current,
            Device = device,
            Trust = _trust.Current,
            IsEnrolled = _enrollment.IsEnrolled,
        };
    }

    public void LoadAll()
    {
        _organizations.Load();
        _users.Load();
        _enrollment.LoadDevice();
        _trust.Load();
    }
}
