package entity_test

import (
	"encoding/json"
	"testing"

	"dataservices/internal/entity"
	"github.com/stretchr/testify/require"
)

func TestEndpoint(t *testing.T) {
	testcases := []struct {
		arg  entity.EndpointArgs
		json string
	}{
		{
			arg: entity.EndpointArgs{
				Name:        "name",
				Type:        "string",
				Description: "unique name",
			},
			json: `{"name":"name","type":"string","value_type":"","value_map":null,"default":"","description":"unique name","pattern":""}`,
		},
		{
			arg: entity.EndpointArgs{
				Name:        "id",
				Type:        "bigint",
				Default:     "1",
				Description: "unique id",
			},
			json: `{"name":"id","type":"bigint","value_type":"","value_map":null,"default":"1","description":"unique id","pattern":""}`,
		},
		{
			arg: entity.EndpointArgs{
				Name:        "v",
				Type:        "float",
				Default:     "3.14",
				Description: "value",
			},
			json: `{"name":"v","type":"float","value_type":"","value_map":null,"default":"3.14","description":"value","pattern":""}`,
		},
		{
			arg: entity.EndpointArgs{
				Name:        "tp",
				Type:        "enum",
				ValueType:   "bigint unsigned",
				Default:     "1",
				Description: "row type",
				Pattern:     "\\w.*",
				ValueMap: map[string]string{
					"1": "",
					"2": "",
					"3": "",
				},
			},
			json: `{"name":"tp","type":"enum","value_type":"bigint unsigned","value_map":{"1":"","2":"","3":""},"default":"1","description":"row type","pattern":"\\w.*"}`,
		},
		{
			arg: entity.EndpointArgs{
				Name:        "names",
				Type:        "array",
				ValueType:   "string",
				Default:     "a,b",
				Description: "needed names array",
			},
			json: `{"name":"names","type":"array","value_type":"string","value_map":null,"default":"a,b","description":"needed names array","pattern":""}`,
		},
		{
			arg: entity.EndpointArgs{
				Name:        "period",
				Type:        "replace",
				ValueType:   "string",
				Default:     "all_times",
				Description: "period condition",
				ValueMap: map[string]string{
					"year_2022":    "AND (created_at BETWEEN '2022-01-01' AND '2022-12-31')",
					"past_1_year":  "AND (created_at BETWEEN DATE_SUB(NOW(), INTERVAL 1 YEAR) AND NOW())",
					"past_3_years": "AND (created_at BETWEEN DATE_SUB(NOW(), INTERVAL 3 YEAR) AND NOW())",
					"all_times":    "",
				},
			},
			json: `{"name":"period","type":"replace","value_type":"string","value_map":{"all_times":"","past_1_year":"AND (created_at BETWEEN DATE_SUB(NOW(), INTERVAL 1 YEAR) AND NOW())","past_3_years":"AND (created_at BETWEEN DATE_SUB(NOW(), INTERVAL 3 YEAR) AND NOW())","year_2022":"AND (created_at BETWEEN '2022-01-01' AND '2022-12-31')"},"default":"all_times","description":"period condition","pattern":""}`,
		},
	}
	for _, ca := range testcases {
		// Test encode to json string
		data, err := json.Marshal(ca.arg)
		require.NoError(t, err)
		require.Equal(t, ca.json, string(data))

		// Test decode from json string
		v := entity.EndpointArgs{}
		err = json.Unmarshal(data, &v)
		require.NoError(t, err)
		require.Equal(t, v, ca.arg)
	}
}
