package sub2api

import (
	"context"
	"fmt"
	"net/http"
)

func (c *Client) CreateChannel(ctx context.Context, req CreateChannelRequest) (ChannelRef, error) {
	var ref ChannelRef
	if err := c.postJSON(ctx, "/api/v1/admin/channels", req, &ref); err != nil {
		return ChannelRef{}, err
	}
	return ref, nil
}

func (c *Client) UpdateChannel(ctx context.Context, channelID string, req CreateChannelRequest) error {
	path := fmt.Sprintf("/api/v1/admin/channels/%s", channelID)
	statusCode, _, body, err := c.perform(ctx, http.MethodPut, path, req)
	if err != nil {
		return err
	}
	if statusCode < http.StatusOK || statusCode >= http.StatusMultipleChoices {
		return newHTTPError(http.MethodPut, path, statusCode, body)
	}
	return nil
}
