package Messages_test

import (
	"encoding/base64"
	"encoding/json"
	"io/ioutil"
	"net/http"
	"testing"

	"git.tovijaeschke.xyz/tovi/Capsule/Backend/Database"
	"git.tovijaeschke.xyz/tovi/Capsule/Backend/Database/Seeder"
	"git.tovijaeschke.xyz/tovi/Capsule/Backend/Models"
	"git.tovijaeschke.xyz/tovi/Capsule/Backend/Tests"
)

func Test_Messages(t *testing.T) {
	client, ts, err := Tests.InitTestEnv()
	if err != nil {
		t.Errorf("Expected nil, recieved %s", err.Error())
		return
	}

	u, err := Database.GetUserByUsername("test")

	userKey, err := Seeder.GenerateAesKey()
	if err != nil {
		t.Errorf("Expected nil, recieved %s", err.Error())
		return
	}

	key, err := Seeder.GenerateAesKey()
	if err != nil {
		t.Errorf("Expected nil, recieved %s", err.Error())
		return
	}

	dataCiphertext, err := key.AesEncrypt([]byte("Test message"))
	if err != nil {
		t.Errorf("Expected nil, recieved %s", err.Error())
		return
	}

	senderIDCiphertext, err := key.AesEncrypt([]byte(u.ID.String()))
	if err != nil {
		t.Errorf("Expected nil, recieved %s", err.Error())
		return
	}

	keyCiphertext, err := userKey.AesEncrypt(
		[]byte(base64.StdEncoding.EncodeToString(key.Key)),
	)
	if err != nil {
		t.Errorf("Expected nil, recieved %s", err.Error())
		return
	}

	pubKey := Seeder.GetPubKey()

	message := Models.Message{
		MessageData: Models.MessageData{
			Data:         base64.StdEncoding.EncodeToString(dataCiphertext),
			SenderID:     base64.StdEncoding.EncodeToString(senderIDCiphertext),
			SymmetricKey: base64.StdEncoding.EncodeToString(keyCiphertext),
		},
		SymmetricKey: base64.StdEncoding.EncodeToString(
			Seeder.EncryptWithPublicKey(userKey.Key, pubKey),
		),
		AssociationKey: "AssociationKey",
	}

	err = Database.CreateMessage(&message)
	if err != nil {
		t.Errorf("Expected nil, recieved %s", err.Error())
		return
	}

	resp, err := client.Get(ts.URL + "/api/v1/auth/messages/AssociationKey")
	if err != nil {
		t.Errorf("Expected user record, recieved %s", err.Error())
		return
	}

	if resp.StatusCode != http.StatusOK {
		t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode)
		return
	}

	requestBody, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Errorf("Expected nil, recieved %s", err.Error())
		return
	}

	var m []Models.Message
	err = json.Unmarshal(requestBody, &m)

	if len(m) != 1 {
		t.Errorf("Expected %d, recieved %d", 1, len(m))
	}

	msg := m[0]

	decodedData, err := base64.StdEncoding.DecodeString(msg.MessageData.Data)
	if err != nil {
		t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode)
		return
	}
	decrypedData, err := key.AesDecrypt(decodedData)
	if err != nil {
		t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode)
		return
	}

	if string(decrypedData) != "Test message" {
		t.Errorf("Expected %s, recieved %s", "Test converation", string(decrypedData))
	}
}

func Test_MessagesPagination(t *testing.T) {
	client, ts, err := Tests.InitTestEnv()
	if err != nil {
		t.Errorf("Expected nil, recieved %s", err.Error())
		return
	}

	u, err := Database.GetUserByUsername("test")

	userKey, err := Seeder.GenerateAesKey()
	if err != nil {
		t.Errorf("Expected nil, recieved %s", err.Error())
		return
	}

	key, err := Seeder.GenerateAesKey()
	if err != nil {
		t.Errorf("Expected nil, recieved %s", err.Error())
		return
	}

	dataCiphertext, err := key.AesEncrypt([]byte("Test message"))
	if err != nil {
		t.Errorf("Expected nil, recieved %s", err.Error())
		return
	}

	senderIDCiphertext, err := key.AesEncrypt([]byte(u.ID.String()))
	if err != nil {
		t.Errorf("Expected nil, recieved %s", err.Error())
		return
	}

	keyCiphertext, err := userKey.AesEncrypt(
		[]byte(base64.StdEncoding.EncodeToString(key.Key)),
	)
	if err != nil {
		t.Errorf("Expected nil, recieved %s", err.Error())
		return
	}

	pubKey := Seeder.GetPubKey()

	for i := 0; i < 50; i++ {
		message := Models.Message{
			MessageData: Models.MessageData{
				Data:         base64.StdEncoding.EncodeToString(dataCiphertext),
				SenderID:     base64.StdEncoding.EncodeToString(senderIDCiphertext),
				SymmetricKey: base64.StdEncoding.EncodeToString(keyCiphertext),
			},
			SymmetricKey: base64.StdEncoding.EncodeToString(
				Seeder.EncryptWithPublicKey(userKey.Key, pubKey),
			),
			AssociationKey: "AssociationKey",
		}

		err = Database.CreateMessage(&message)
		if err != nil {
			t.Errorf("Expected nil, recieved %s", err.Error())
			return
		}
	}

	resp, err := client.Get(ts.URL + "/api/v1/auth/messages/AssociationKey")
	if err != nil {
		t.Errorf("Expected user record, recieved %s", err.Error())
		return
	}

	if resp.StatusCode != http.StatusOK {
		t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode)
		return
	}

	requestBody, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Errorf("Expected nil, recieved %s", err.Error())
		return
	}

	var m []Models.Message
	err = json.Unmarshal(requestBody, &m)

	if len(m) != 20 {
		t.Errorf("Expected %d, recieved %d", 20, len(m))
	}
}
