using RackPeek.Domain.Persistence;

namespace RackPeek.Domain.Resources.SystemResources.UseCases;

public record SystemReport(
    IReadOnlyList<SystemReportRow> Systems
);

public record SystemReportRow(
    string Name,
    string? Type,
    string? Os,
    int Cores,
    double RamGb,
    int TotalStorageGb,
    List<string> RunsOn
);

public class SystemReportUseCase(IResourceCollection repository) : IUseCase {
    public async Task<SystemReport> ExecuteAsync() {
        IReadOnlyList<SystemResource> systems = await repository.GetAllOfTypeAsync<SystemResource>();

        var rows = systems.Select(system => {
            var totalStorage = system.Drives?.Sum(d => d.Size) ?? 0;

            return new SystemReportRow(
                system.Name,
                system.Type,
                system.Os,
                system.Cores ?? 0,
                system.Ram ?? 0,
                totalStorage,
                system.RunsOn
            );
        }).ToList();

        return new SystemReport(rows);
    }
}
