package integration

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
)

func (c *client) vkusvillSaveCandidate(method, url string, request *vkusvillSaveCandidateRequest) (*http.Response, error) {
	secrets, ok := c.secrets[VkusvillCompanyName].(VkusvillSecretsConfig)
	if !ok {
		return nil, fmt.Errorf("vkusvill secrets are not of expected type")
	}

	body, err := json.Marshal(request)
	if err != nil {
		return nil, fmt.Errorf("error marshaling request: %w", err)
	}

	req, err := http.NewRequest(method, url, bytes.NewBuffer(body))
	if err != nil {
		return nil, fmt.Errorf("error creating request: %w", err)
	}

	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("User-Agent", "Molva/1.0")
	req.Header.Set("Authorization", secrets.ApiToken)

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return nil, fmt.Errorf("error sending request: %w", err)
	}

	return resp, nil
}
