package sub2api

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

func (c *Client) DeleteGroup(ctx context.Context, groupID string) error {
	return c.deleteResource(ctx, "/api/v1/admin/groups/", groupID)
}

func (c *Client) DeleteChannel(ctx context.Context, channelID string) error {
	return c.deleteResource(ctx, "/api/v1/admin/channels/", channelID)
}

func (c *Client) DeletePlan(ctx context.Context, planID string) error {
	return c.deleteResource(ctx, "/api/v1/admin/payment/plans/", planID)
}

func (c *Client) DeleteAccount(ctx context.Context, accountID string) error {
	return c.deleteResource(ctx, "/api/v1/admin/accounts/", accountID)
}

func (c *Client) deleteResource(ctx context.Context, prefix, resourceID string) error {
	resourceID = strings.TrimSpace(resourceID)
	if resourceID == "" {
		return fmt.Errorf("resource id is required")
	}
	path := prefix + resourceID
	statusCode, _, body, err := c.perform(ctx, http.MethodDelete, path, nil)
	if err != nil {
		return err
	}
	if statusCode < http.StatusOK || statusCode >= http.StatusMultipleChoices {
		return newHTTPError(http.MethodDelete, path, statusCode, body)
	}
	return nil
}
