package controllers_test

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"testing"

	"github.com/stretchr/testify/assert"
	"gitlab.com/pactual1/backend/shared"
)

func TestListTextTemplates(t *testing.T) {
	ts := runTestServer()
	defer ts.Close()
	defer shared.CloseDb()

	t.Run("it should return 200", func(t *testing.T) {
		resp, err := http.Get(fmt.Sprintf("%s/templates?limit=20", ts.URL))

		if err != nil {
			t.Fatalf("Expected no error, got %v", err)
		}

		assert.Equal(t, 200, resp.StatusCode)
	})
}

func CreateTextTemplate(t *testing.T) {
	ts := runTestServer()
	defer ts.Close()
	defer shared.CloseDb()

	t.Run("it should return 200", func(t *testing.T) {
		// Add a new text template
		data := map[string]interface{}{
			"Value": "text",
		}

		payload, _ := json.Marshal(data)
		resp, err := http.Post(fmt.Sprintf("%s/templates/save", ts.URL), "application/json", bytes.NewBuffer(payload))
		if err != nil {
			t.Fatalf("Expected no error, got %v", err)
		}

		assert.Equal(t, 200, resp.StatusCode)

	})
}
