package service

import (
	"encoding/json"
	"log/slog"
	"net/http"
	"xoxo-webserver/internal/domain/model"
)

// Alternative APIs and according documentation:
// https://taylor-swift-api.sarbo.workers.dev/lyrics?numberOfParagraphs=1&shouldRandomizeLyrics=true
// https://taylorswiftapi.onrender.com/get
// https://github.com/MitanshiKshatriya/taylor-swift-api
// https://taylorswiftapi.onrender.com
// https://bump.sh/sarbor/doc/taylor-swift-api

const serverDomain = "https://taylor-swift-api.vercel.app"

func GetRandomQuote() (*model.Quote, error) {
	r, err := http.Get(serverDomain + "/api/quotes")
	if err != nil {
		slog.Error("Could not fetch quote", "serverDomain", serverDomain, "err", err)
		return nil, err
	}
	quote := &model.Quote{}
	d := json.NewDecoder(r.Body)
	err = d.Decode(quote)
	if err != nil {
		slog.Error("Could not parse result of service API", "err", err)
		return nil, err
	}
	return quote, nil
}
