// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package omnivore // import "miniflux.app/v2/internal/integration/omnivore"

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"time"

	"miniflux.app/v2/internal/config"
	"miniflux.app/v2/internal/crypto"
	"miniflux.app/v2/internal/http/client"
	"miniflux.app/v2/internal/version"
)

const defaultClientTimeout = 10 * time.Second

const defaultApiEndpoint = "https://api-prod.omnivore.app/api/graphql"

var mutation = `
mutation SaveUrl($input: SaveUrlInput!) {
  saveUrl(input: $input) {
    ... on SaveSuccess {
      url
      clientRequestId
    }
    ... on SaveError {
      errorCodes
      message
    }
  }
}
`

type errorResponse struct {
	Errors []struct {
		Message string `json:"message"`
	} `json:"errors"`
}

type successResponse struct {
	Data struct {
		SaveUrl struct {
			URL             string `json:"url"`
			ClientRequestID string `json:"clientRequestId"`
		} `json:"saveUrl"`
	} `json:"data"`
}

type Client struct {
	wrapped     *http.Client
	apiEndpoint string
	apiToken    string
}

func NewClient(apiToken string, apiEndpoint string) *Client {
	if apiEndpoint == "" {
		apiEndpoint = defaultApiEndpoint
	}

	return &Client{wrapped: client.NewClientWithOptions(client.Options{Timeout: defaultClientTimeout, BlockPrivateNetworks: !config.Opts.IntegrationAllowPrivateNetworks()}), apiEndpoint: apiEndpoint, apiToken: apiToken}
}

func (c *Client) SaveURL(url string) error {
	var payload = map[string]any{
		"query": mutation,
		"variables": map[string]any{
			"input": map[string]any{
				"clientRequestId": crypto.GenerateUUID(),
				"source":          "api",
				"url":             url,
			},
		},
	}
	b, err := json.Marshal(payload)
	if err != nil {
		return err
	}
	req, err := http.NewRequest(http.MethodPost, c.apiEndpoint, bytes.NewReader(b))
	if err != nil {
		return err
	}

	req.Header.Set("Authorization", c.apiToken)
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("User-Agent", "Miniflux/"+version.Version)

	resp, err := c.wrapped.Do(req)
	if err != nil {
		return err
	}

	defer resp.Body.Close()
	b, err = io.ReadAll(resp.Body)
	if err != nil {
		return fmt.Errorf("omnivore: failed to parse response: %v", err)
	}

	if resp.StatusCode >= 400 {
		var errResponse errorResponse
		if err = json.Unmarshal(b, &errResponse); err != nil {
			return fmt.Errorf("omnivore: failed to save URL: status=%d %s", resp.StatusCode, string(b))
		}
		if len(errResponse.Errors) > 0 {
			return fmt.Errorf("omnivore: failed to save URL: status=%d %s", resp.StatusCode, errResponse.Errors[0].Message)
		}
		return fmt.Errorf("omnivore: failed to save URL: status=%d %s", resp.StatusCode, string(b))
	}

	var successResp successResponse
	if err = json.Unmarshal(b, &successResp); err != nil {
		return fmt.Errorf("omnivore: failed to parse response, however the request appears successful, is the url correct?: status=%d %s", resp.StatusCode, string(b))
	}

	return nil
}
