package json

import (
	"amocrm/internal/models"
	"amocrm/internal/tools"
	"context"
	"encoding/json"
	"fmt"
	"github.com/stretchr/testify/assert"
	"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal"
	"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
	"testing"
)

func TestUnmarshalJSON(t *testing.T) {
	jsonData := []byte(`[
    {
        "field_id": 1,
        "values": [
            {"value": "Value1"},
            {"value": {"file_uuid": "123e4567file_uuid", "version_uuid": "123e4567version_uuid", "file_name": "file.pdf", "file_size": 1}}
        ]
    },
    {
        "field_id": 2,
        "values": [
            {"value": "Value2"},
            {"value": {"file_uuid": "98765432file_uuid", "version_uuid": "98765432version_uuid", "file_name": "file.wc", "file_size": 2}}
        ]
    },
    {
        "field_id": 3,
        "values": [
            {"value": "Value3"},
            {"value": {"file_uuid": "abcdeffile_uuid", "version_uuid": "abcdefversion_uuid", "file_name": "file.txt", "file_size": 3}}
        ]
    }
]
`)

	var fv []models.FieldsValues
	err := json.Unmarshal(jsonData, &fv)
	if err != nil {
		t.Errorf("UnmarshalJSON failed: %v", err)
	}

	for _, f := range fv {
		fmt.Println(f)
	}
	assert.Equal(t, 3, len(fv))

	jsonAgain, err := json.Marshal(fv)
	assert.NoError(t, err)

	fmt.Println(string(jsonAgain))
}

func Test_ContactRule(t *testing.T) {
	ctx := context.Background()
	repo, err := dal.NewAmoDal(ctx, "host=localhost port=35432 user=squiz password=Redalert2 dbname=squiz sslmode=disable")
	assert.NoError(t, err)

	accountID := "64f2cd7a7047f28fdabf6d9e"

	quiz, err := repo.QuizRepo.GetQuizById(ctx, accountID, 26166)
	assert.NoError(t, err)

	var quizConfig model.QuizContact
	err = json.Unmarshal([]byte(quiz.Config), &quizConfig)
	assert.NoError(t, err)

	currentFields, err := repo.AmoRepo.GetUserFieldsByID(ctx, 30228997)
	assert.NoError(t, err)

	contactFieldsToCreate, forAdding := tools.ForContactRules(quizConfig, currentFields)

	fmt.Println("contactFieldsToCreate", contactFieldsToCreate)
	fmt.Println("forAdding", forAdding)
}
