using Nexus.Contracts.Devices;

namespace NexusIdentity.Api.Models;

public sealed class OrganizationEntity
{
    public string Id { get; set; } = string.Empty;
    public string Name { get; set; } = string.Empty;
    public DateTime CreatedAtUtc { get; set; } = DateTime.UtcNow;
    public bool IsActive { get; set; } = true;

    public ICollection<UserEntity> Users { get; set; } = [];
    public ICollection<DeviceEntity> Devices { get; set; } = [];
    public ICollection<PolicyEntity> Policies { get; set; } = [];
}

public sealed class DeviceEntity
{
    public string DeviceId { get; set; } = string.Empty;
    public string? OrganizationId { get; set; }
    public OrganizationEntity? Organization { get; set; }
    public Guid? UserId { get; set; }
    public UserEntity? User { get; set; }
    public string ComputerName { get; set; } = string.Empty;
    public string FriendlyName { get; set; } = string.Empty;
    public string Owner { get; set; } = string.Empty;
    public string Os { get; set; } = string.Empty;
    public string AgentVersion { get; set; } = string.Empty;
    public string ShellVersion { get; set; } = string.Empty;
    public string EnrollmentStatus { get; set; } = "Unknown";
    public DateTime RegisteredAtUtc { get; set; } = DateTime.UtcNow;
    public DateTime? LastSeenUtc { get; set; }
    public bool Online { get; set; }
    public string LastHealth { get; set; } = "Healthy";

    public DeviceTrustEntity? Trust { get; set; }
    public DevicePresenceEntity? Presence { get; set; }
    public ICollection<DeviceHeartbeatEntity> Heartbeats { get; set; } = [];
}

public sealed class DevicePresenceEntity
{
    public string DeviceId { get; set; } = string.Empty;
    public DeviceEntity Device { get; set; } = null!;
    public bool Online { get; set; }
    public HealthStatus HealthStatus { get; set; } = HealthStatus.Healthy;
    public DateTime? LastSeenUtc { get; set; }
    public DateTime? LastHeartbeatUtc { get; set; }
    public string UserName { get; set; } = string.Empty;
    public string OrganizationName { get; set; } = string.Empty;
    public string AgentVersion { get; set; } = string.Empty;
    public string ShellVersion { get; set; } = string.Empty;
    public DateTime UpdatedAtUtc { get; set; } = DateTime.UtcNow;
}

public sealed class DeviceHeartbeatEntity
{
    public Guid Id { get; set; }
    public string DeviceId { get; set; } = string.Empty;
    public DeviceEntity Device { get; set; } = null!;
    public DateTime ReceivedAtUtc { get; set; } = DateTime.UtcNow;
    public bool Online { get; set; }
    public HealthStatus HealthStatus { get; set; } = HealthStatus.Healthy;
    public string UserName { get; set; } = string.Empty;
    public string OrganizationName { get; set; } = string.Empty;
    public string AgentVersion { get; set; } = string.Empty;
    public string ShellVersion { get; set; } = string.Empty;
}

public sealed class DeviceTrustEntity
{
    public string DeviceId { get; set; } = string.Empty;
    public DeviceEntity Device { get; set; } = null!;
    public string Status { get; set; } = "Pending";
    public DateTime? EvaluatedAtUtc { get; set; }
    public string? Reason { get; set; }
}

public sealed class EnrollmentRequestEntity
{
    public Guid Id { get; set; }
    public string DeviceId { get; set; } = string.Empty;
    public string OrganizationId { get; set; } = string.Empty;
    public OrganizationEntity? Organization { get; set; }
    public string UserName { get; set; } = string.Empty;
    public string DeviceFriendlyName { get; set; } = string.Empty;
    public string ComputerName { get; set; } = string.Empty;
    public string ShellVersion { get; set; } = string.Empty;
    public string AgentVersion { get; set; } = string.Empty;
    public string Status { get; set; } = "Pending";
    public DateTime RequestedAtUtc { get; set; } = DateTime.UtcNow;
    public DateTime? ProcessedAtUtc { get; set; }
}

public sealed class PolicyEntity
{
    public string Id { get; set; } = string.Empty;
    public string Name { get; set; } = string.Empty;
    public string Scope { get; set; } = "platform";
    public bool IsEnabled { get; set; } = true;
    public string Summary { get; set; } = string.Empty;
    public string RulesJson { get; set; } = "{}";
    public string? OrganizationId { get; set; }
    public OrganizationEntity? Organization { get; set; }
    public DateTime CreatedAtUtc { get; set; } = DateTime.UtcNow;
}
