// Code generated by django-cfg/django_client - DO NOT EDIT.
// Generated at: {{ generated_at }}

package {{ package_name }}

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

// Client is the HTTP API client.
type Client struct {
	BaseURL    string
	HTTPClient *http.Client
	Token      string // Bearer token for authentication
}

// Option is a functional option for configuring the Client.
type Option func(*Client)

// WithHTTPClient sets a custom HTTP client.
func WithHTTPClient(client *http.Client) Option {
	return func(c *Client) {
		c.HTTPClient = client
	}
}

// WithToken sets the authentication token.
func WithToken(token string) Option {
	return func(c *Client) {
		c.Token = token
	}
}

// NewClient creates a new API client.
func NewClient(baseURL string, options ...Option) *Client {
	client := &Client{
		BaseURL:    baseURL,
		HTTPClient: &http.Client{},
	}

	for _, opt := range options {
		opt(client)
	}

	return client
}

// doRequest performs HTTP request with error handling.
func (c *Client) doRequest(ctx context.Context, method, path string, body interface{}, response interface{}) error {
	var reqBody io.Reader

	if body != nil {
		data, err := json.Marshal(body)
		if err != nil {
			return fmt.Errorf("failed to marshal request body: %w", err)
		}
		reqBody = bytes.NewReader(data)
	}

	req, err := http.NewRequestWithContext(ctx, method, c.BaseURL+path, reqBody)
	if err != nil {
		return fmt.Errorf("failed to create request: %w", err)
	}

	req.Header.Set("Content-Type", "application/json")
	if c.Token != "" {
		req.Header.Set("Authorization", "Bearer "+c.Token)
	}

	resp, err := c.HTTPClient.Do(req)
	if err != nil {
		return fmt.Errorf("HTTP request failed: %w", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode >= 400 {
		var apiErr APIError
		apiErr.StatusCode = resp.StatusCode
		if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil {
			apiErr.Message = resp.Status
		}
		return &apiErr
	}

	if response != nil {
		if err := json.NewDecoder(resp.Body).Decode(response); err != nil {
			return fmt.Errorf("failed to decode response: %w", err)
		}
	}

	return nil
}

{% for operation in operations %}
// {{ operation.name }} - {{ operation.description }}
func (c *Client) {{ operation.name }}(ctx context.Context{% for param in operation.parameters %}, {{ param.name }} {{ param.type }}{% endfor %}{% if operation.request_type %}, req *{{ operation.request_type }}{% endif %}) ({% if operation.response_type != 'interface{}' %}*{{ operation.response_type }}{% else %}interface{}{% endif %}, error) {
	{% if operation.response_type != 'interface{}' %}var response {{ operation.response_type }}{% endif %}

	path := "{{ operation.path }}"
	{% if operation.parameters %}
	// TODO: Build path with parameters and query string
	{% endif %}

	err := c.doRequest(ctx, "{{ operation.http_method }}", path, {% if operation.request_type %}req{% else %}nil{% endif %}, {% if operation.response_type != 'interface{}' %}&response{% else %}nil{% endif %})
	if err != nil {
		return nil, err
	}

	{% if operation.response_type != 'interface{}' %}
	return &response, nil
	{% else %}
	return nil, nil
	{% endif %}
}

{% endfor %}
