package amoClient

import (
	"amocrm/internal/models"
	"amocrm/internal/workers/limiter"
	"context"
	"encoding/json"
	"fmt"
	"github.com/gofiber/fiber/v2"
	"github.com/stretchr/testify/assert"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"testing"
	"time"
)

func Test_CreateWebhook(t *testing.T) {
	ctx := context.Background()
	cfgLogger := zap.NewDevelopmentConfig()
	cfgLogger.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
	cfgLogger.EncoderConfig.ConsoleSeparator = "    "

	logger, err := cfgLogger.Build()
	if err != nil {
		fmt.Println(err)
	}

	rateLimiter := limiter.NewRateLimiter(ctx, 6, 1500*time.Millisecond)

	amoclient := NewAmoClient(AmoDeps{
		Logger:            logger,
		RedirectionURL:    "https://squiz.pena.digital/squiz/amocrm/oauth",
		IntegrationID:     "2dbd6329-9be6-41f2-aa5f-964b9e723e49",
		IntegrationSecret: "tNK3LwL4ovP0OBK4jKDHJ3646PqRJDOKQYgY6P2t6DCuV8LEzDzszTDY0Fhwmzc8",
		RateLimiter:       rateLimiter,
	})

	req2 := models.CreateWebHookReq{
		GrantType: "authorization_code",
		Code:      "def502003378c2a850f6f3b9618ee811a371e0552692060dd206e166244d8508f1df34e4870e12d183777d29e275fc2e1a5680f27f3999715be63a429a40bef4980ed28f03989b2acc90ac4f8a7a11b514a246a564170d0349ea1ec6584ba8f636ad0d856d6e9ed75e472d461ceee40052513335b9738d5782570a75ec7b4cb3c9bfcf564d93a30e548cff96c789b6097f5c4e254139dc829083ccc5395c276e1b29cd001b8f0efa5579b9e989caeaeb895a6602d70254715b969aa5ce8cd91fc379b406877f3d3258702c4f1f8ca6c8b52eed492aec209418801626e50a1b9b04f4346de452f20e7b4d9611a58e8742342481234a161662e35340aba3aedcb1616fac4be6a125fc6d2aa25ab04eed394ed3ee8f9749ed32048ce69a932f83cdd1fe4d8788ac28683698b729b7d4c36ba6a045d3dc488f5da968ddc4837bdb6a26d4e3f5abcb58c8175c3ab20c6c3bad13c613c77ef23484c2a1ebd4a2152168b15f8d21feafa3178cececdbd47f91863d715f5905b0385efa0744692d863a768aa431b07ea667fef134d3c3a749efdc064d74887a889219e68fd34ab435eb761fea6415f4c4760dd8887b8978d62a35e745826edac41019539012592f737ed5ca690b72ce06c7a2486847b95d47a157f0965eaad4839fd7d1927c03c6152f438dd92a465f58e753523965ac127abd7354",
	}
	resp, err := amoclient.CreateWebHook(&req2, "penadigitaltech.amocrm.ru")
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println(resp)
}

func Test_CustomersMode(t *testing.T) {
	fiberClient := fiber.AcquireClient()
	uri := fmt.Sprint("https://penadigitaltech.amocrm.ru/api/v4/customers/mode")
	bodyBytes, err := json.Marshal(struct {
		Mode      string `json:"mode"`
		ISenabled bool   `json:"is_enabled"`
	}{
		Mode:      "segments",
		ISenabled: true,
	})
	if err != nil {
		fmt.Println(err)
	}
	agent := fiberClient.Patch(uri)
	agent.Set("Content-Type", "application/json").Body(bodyBytes)
	agent.Set("Authorization", "Bearer "+"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjFkMmZlNjFlMjFkZGZjOGRmN2MyNWRhYjc3MTJkMDM5MDVhZWEwYzkyYjJmZTU0Njc4OTgzYmUzM2Q0NWExYjQ2YmI1ZDY2MjJkNDA2Y2ZjIn0.eyJhdWQiOiIyZGJkNjMyOS05YmU2LTQxZjItYWE1Zi05NjRiOWU3MjNlNDkiLCJqdGkiOiIxZDJmZTYxZTIxZGRmYzhkZjdjMjVkYWI3NzEyZDAzOTA1YWVhMGM5MmIyZmU1NDY3ODk4M2JlMzNkNDVhMWI0NmJiNWQ2NjIyZDQwNmNmYyIsImlhdCI6MTcxNDY0NDA3OSwibmJmIjoxNzE0NjQ0MDc5LCJleHAiOjE3MTQ3MzA0NzksInN1YiI6IjgxMTA3MjYiLCJncmFudF90eXBlIjoiIiwiYWNjb3VudF9pZCI6MzAyMjg5OTcsImJhc2VfZG9tYWluIjoiYW1vY3JtLnJ1IiwidmVyc2lvbiI6Miwic2NvcGVzIjpbInB1c2hfbm90aWZpY2F0aW9ucyIsImNybSIsIm5vdGlmaWNhdGlvbnMiXSwiaGFzaF91dWlkIjoiZWUyNjFjMDgtOGZiMC00NTkzLTlmNmQtOWVhNDFhZTljNTJhIn0.qUaJwH95LnU3xMA4GMQ0wtK1_vA_bMM8kd5BlRYNpL6ohEhl4CPk8EdR0qXmtBonsh4Z2kwXXAcPtiysZ6XA4kO1JLrgMN3cxthwEO2Z3UxI5O0L5W3DJvCco_4PCbRgUZWxlrR48NxmZ_bWkrhQb07txygvOOhB2T6lpX2CnkDPlS914jYP9QT8BBREEkERbr1zehVSt4NCCXNSC_Tnj9uXOj5GeYnm2Sw5OUXKEJspmDKEOoX4m_FKwlg3ywQfbWKDemVQYuHgmaPalDLLnAC8iydE50NLol07pQvhkK8zOjgz_zif7vENFH4152P9-ltFEvJVmwyqoN23Xuo7Aw")

	statusCode, resBody, errs := agent.Bytes()
	if len(errs) > 0 {
		fmt.Printf("request failed: %v", errs[0])
	}

	if statusCode != fiber.StatusOK {
		errorMessage := fmt.Sprintf("received an incorrect response from AddLeadsFields: %s", string(resBody))
		fmt.Println(errorMessage)
	}

	fmt.Println(string(resBody))
}

func Test_GetUserInfo(t *testing.T) {
	ctx := context.Background()
	cfgLogger := zap.NewDevelopmentConfig()
	cfgLogger.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
	cfgLogger.EncoderConfig.ConsoleSeparator = "    "

	logger, err := cfgLogger.Build()
	if err != nil {
		fmt.Println(err)
	}

	rateLimiter := limiter.NewRateLimiter(ctx, 6, 1500*time.Millisecond)

	amoclient := NewAmoClient(AmoDeps{
		Logger:            logger,
		RedirectionURL:    "https://squiz.pena.digital/squiz/amocrm/oauth",
		IntegrationID:     "2dbd6329-9be6-41f2-aa5f-964b9e723e49",
		IntegrationSecret: "tNK3LwL4ovP0OBK4jKDHJ3646PqRJDOKQYgY6P2t6DCuV8LEzDzszTDY0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LP7sgLUA4tA", "penadigitaltech.amocrm.ru")
	assert.NoError(t, err)

	fmt.Println(resp)
}

func Test_DealGet(t *testing.T) {
	url := "https://penadigitaltech.amocrm.ru/api/v4/leads/44690375"

	client := fiber.AcquireClient()

	agent := client.Get(url)
	agent.Set("Authorization", "Bearer "+"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjkxNjQxYzdiMGEzYTRjZGUzZmUxYmNiZDZiZmNhMDYxNDcyOTczMGIwZDMxNWIyYWFlYWJhYTRmNDAyMzlhYjZhYWNlZjQzMGQwNGIxMmY1In0.eyJhdWQiOiIyZGJkNjMyOS05YmU2LTQxZjItYWE1Zi05NjRiOWU3MjNlNDkiLCJqdGkiOiI5MTY0MWM3YjBhM2E0Y2RlM2ZlMWJjYmQ2YmZjYTA2MTQ3Mjk3MzBiMGQzMTViMmFhZWFiYWE0ZjQwMjM5YWI2YWFjZWY0MzBkMDRiMTJmNSIsImlhdCI6MTcxODY4MzIwMCwibmJmIjoxNzE4NjgzMjAwLCJleHAiOjE3MTg3Njk2MDAsInN1YiI6Ijg0MTM5NjkiLCJncmFudF90eXBlIjoiIiwiYWNjb3VudF9pZCI6MzAyMjg5OTcsImJhc2VfZG9tYWluIjoiYW1vY3JtLnJ1IiwidmVyc2lvbiI6Miwic2NvcGVzIjpbInB1c2hfbm90aWZpY2F0aW9ucyIsImZpbGVzIiwiY3JtIiwibm90aWZpY2F0aW9ucyJdLCJoYXNoX3V1aWQiOiJhMzBjOWQ1MS1hYTM4LTRlMDYtYWNlMy1iYTQ5MmE3NjE1ZmUifQ.mclcK1MHEIYG9nDSO6NdXIyvIKzd-2h7OrmE-7JjWpIj4WO9W6jUsIEwuJs8glbRT1wvf-SBV9p1Di1QSZE2-9k6exi7W6xgzoK1xLeukdcFd3yTEpDXfamBaMIvlAOyJQ8ZjyqE3Y3f983jUiabe_gGAEk8JxgQzkVNtmvjgeaf8qbHyAwPZ98DddrL91airQooEaT3kmqmXo9R1X0TCfMOh_23xqGH7TDrlQ0AuDo-VjUh5Merc_z6atAocSc1HwHpnjHgMj9Ib3KIenDqbeiUk4evtsOpDO2VdLrKQTPicQUDQGxShaiexF0oGVnoclFh2Cymqon_FuQDnnuyvg")

	statusCode, resBody, errs := agent.Bytes()
	if len(errs) > 0 {
		fmt.Println(errs)
	}

	if statusCode != fiber.StatusOK {
		fmt.Println(statusCode)
	}

	fmt.Println(string(resBody))
}
