package sub2api

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

func (c *Client) DisableOpenAIResponsesAPI(ctx context.Context, accountIDs []string) error {
	seen := map[string]struct{}{}
	for _, rawID := range accountIDs {
		accountID := strings.TrimSpace(rawID)
		if accountID == "" {
			continue
		}
		if _, ok := seen[accountID]; ok {
			continue
		}
		seen[accountID] = struct{}{}

		path := "/api/v1/admin/accounts/" + accountID
		payload := map[string]any{
			"extra": map[string]any{
				"openai_responses_supported": false,
			},
		}
		statusCode, _, body, err := c.perform(ctx, http.MethodPut, path, payload)
		if err != nil {
			return err
		}
		if statusCode < http.StatusOK || statusCode >= http.StatusMultipleChoices {
			return newHTTPError(http.MethodPut, path, statusCode, body)
		}
	}
	return nil
}

func (c *Client) ClearTempUnschedulable(ctx context.Context, accountIDs []string) error {
	seen := map[string]struct{}{}
	for _, rawID := range accountIDs {
		accountID := strings.TrimSpace(rawID)
		if accountID == "" {
			continue
		}
		if _, ok := seen[accountID]; ok {
			continue
		}
		seen[accountID] = struct{}{}

		path := "/api/v1/admin/accounts/" + accountID + "/temp-unschedulable"
		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
}
