/*
 * Copyright (c) 2022 Andrew Ayer
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Except as contained in this notice, the name(s) of the above copyright
 * holders shall not be used in advertising or otherwise to promote the
 * sale, use or other dealings in this Software without prior written
 * authorization.
 */

package voipms

import (
	"context"
	"encoding/json"
	"fmt"
	"io"
	"log"
	"net/http"
	"net/url"
)

type apiResponse struct {
	Status string `json:"status"`
}

func doRequest(ctx context.Context, form url.Values) (*apiResponse, error) {
	// GET https://voip.ms/api/v1/rest.php?{form}
	// output will contain {"status":"success"} if successful

	req, err := http.NewRequestWithContext(ctx, "GET", "https://voip.ms/api/v1/rest.php?"+form.Encode(), nil)
	if err != nil {
		return nil, err
	}
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return nil, err
	}
	respBytes, err := io.ReadAll(resp.Body)
	resp.Body.Close()
	if err != nil {
		return nil, fmt.Errorf("error reading response from voip.ms: %w", err)
	}

	log.Printf("received from voip.ms %q", string(respBytes))

	if contentType := resp.Header.Get("Content-Type"); contentType != "application/json" {
		return nil, fmt.Errorf("received non-JSON response %v from voip.ms", contentType)
	}

	apiResp := new(apiResponse)
	if err := json.Unmarshal(respBytes, apiResp); err != nil {
		return nil, err
	}
	return apiResp, nil
}
