package methods

import (
	"encoding/json"
	"errors"
	"io"
	"net/http"
	"sevenkeys/scryfall/types"
)

var BULK_DATA_URI = "https://api.scryfall.com/bulk-data"

func GetBulkDataByType(bulkDataType string) (types.BulkData, error) {
	response, err := http.Get(BULK_DATA_URI + "/" + bulkDataType)
	if err != nil {
		return types.BulkData{}, err
	}

	if response.StatusCode != http.StatusOK {
		return types.BulkData{}, errors.New("HTTP request failed with code: " + string(response.StatusCode))
	}

	defer response.Body.Close()
	bulkDataBytes, err := io.ReadAll(response.Body)
	if err != nil {
		return types.BulkData{}, err
	}

	var bulkData types.BulkData
	err = json.Unmarshal(bulkDataBytes, &bulkData)
	if err != nil {
		return types.BulkData{}, err
	}

	return bulkData, nil
}
