import 'dart:async';
import 'dart:convert';

import 'package:dio/dio.dart';

class HelmChart {
  final String name;
  final String version;

  const HelmChart({
    required this.name,
    required this.version,
  });
}

Future<List<HelmChart>> fetchCharts() async {
  final dio = Dio();
  final response = await dio.get('https://helm.badhouseplants.net/api/charts',
      options: Options(headers: {
        "Accept": "application/json",
      }));
  if (response.statusCode == 200) {
    final Map<dynamic, dynamic> charsRaw = json.decode(response.data);

    List<HelmChart> charts = [];
    charsRaw.forEach((key, value) {
      charts.add(HelmChart(name: key, version: value.first["version"]));
    });
    return charts;
  } else {
    throw Exception('Failed to load album');
  }
}
