using EntKube.Clusters.Domain;

namespace EntKube.Clusters.Infrastructure;

/// <summary>
/// In-memory implementation of the cluster repository for local development
/// and testing. Production will replace this with an EF Core or Dapper
/// implementation backed by PostgreSQL.
/// </summary>
public class InMemoryClusterRepository : IClusterRepository
{
    private readonly List<KubernetesCluster> clusters = new();

    public Task<KubernetesCluster?> GetByIdAsync(Guid id, CancellationToken ct = default)
    {
        KubernetesCluster? cluster = clusters.FirstOrDefault(c => c.Id == id);
        return Task.FromResult(cluster);
    }

    public Task<IReadOnlyList<KubernetesCluster>> GetAllAsync(CancellationToken ct = default)
    {
        IReadOnlyList<KubernetesCluster> result = clusters.AsReadOnly();
        return Task.FromResult(result);
    }

    public Task AddAsync(KubernetesCluster cluster, CancellationToken ct = default)
    {
        clusters.Add(cluster);
        return Task.CompletedTask;
    }

    public Task UpdateAsync(KubernetesCluster cluster, CancellationToken ct = default)
    {
        // In-memory: the reference is already updated since we store the object directly.
        return Task.CompletedTask;
    }

    public Task DeleteAsync(Guid id, CancellationToken ct = default)
    {
        clusters.RemoveAll(c => c.Id == id);
        return Task.CompletedTask;
    }
}
