// Copyright 2025 Clidey, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package llm

import (
	"bytes"
	"net/http"

	"github.com/clidey/whodb/core/src/log"
)

func sendHTTPRequest(method, url string, body []byte, headers map[string]string) (*http.Response, error) {
	req, err := http.NewRequest(method, url, bytes.NewBuffer(body))
	if err != nil {
		log.Logger.WithError(err).Errorf("Failed to create HTTP request: method=%s, url=%s", method, url)
		return nil, err
	}

	for key, value := range headers {
		req.Header.Set(key, value)
	}

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		log.Logger.WithError(err).Errorf("HTTP request failed: method=%s, url=%s", method, url)
		return nil, err
	}

	log.Logger.Infof("HTTP request completed: method=%s, url=%s, status=%d", method, url, resp.StatusCode)
	return resp, nil
}
