package Friends_test

import (
	"bytes"
	"encoding/base64"
	"encoding/json"
	"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_CreateFriendRequest(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")
	if err != nil {
		t.Errorf("Expected nil, recieved %s", err.Error())
		return
	}

	u2, err := Tests.InitTestCreateUser("test2")
	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
	}

	decodedPublicKey := Seeder.GetPubKey()

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

	friendReq := Models.FriendRequest{
		UserID: u.ID,
		FriendID: base64.StdEncoding.EncodeToString(
			Seeder.EncryptWithPublicKey(
				[]byte(u2.ID.String()),
				decodedPublicKey,
			),
		),
		FriendUsername: base64.StdEncoding.EncodeToString(
			Seeder.EncryptWithPublicKey(
				[]byte(u2.Username),
				decodedPublicKey,
			),
		),
		FriendPublicAsymmetricKey: base64.StdEncoding.EncodeToString(
			encPublicKey,
		),
		SymmetricKey: base64.StdEncoding.EncodeToString(
			Seeder.EncryptWithPublicKey(key.Key, decodedPublicKey),
		),
	}

	jsonStr, _ := json.Marshal(friendReq)
	req, _ := http.NewRequest("POST", ts.URL+"/api/v1/auth/friend_request", bytes.NewBuffer(jsonStr))
	req.Header.Set("Content-Type", "application/json")

	resp, err := client.Do(req)
	if err != nil {
		t.Errorf("Expected nil, recieved %s", err.Error())
		return
	}

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

	var r Models.FriendRequest

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

	if r.AcceptedAt.Valid == true {
		t.Errorf("Expected false, recieved true")
		return
	}
}

func Test_CreateFriendRequestQrCode(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")
	if err != nil {
		t.Errorf("Expected nil, recieved %s", err.Error())
		return
	}

	u2, err := Tests.InitTestCreateUser("test2")
	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
	}

	decodedPublicKey := Seeder.GetPubKey()

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

	friendReq := Models.FriendRequest{
		UserID: u.ID,
		FriendID: base64.StdEncoding.EncodeToString(
			Seeder.EncryptWithPublicKey(
				[]byte(u2.ID.String()),
				decodedPublicKey,
			),
		),
		FriendUsername: base64.StdEncoding.EncodeToString(
			Seeder.EncryptWithPublicKey(
				[]byte(u2.Username),
				decodedPublicKey,
			),
		),
		FriendPublicAsymmetricKey: base64.StdEncoding.EncodeToString(
			encPublicKey,
		),
		SymmetricKey: base64.StdEncoding.EncodeToString(
			Seeder.EncryptWithPublicKey(key.Key, decodedPublicKey),
		),
	}

	friendReq2 := Models.FriendRequest{
		UserID: u2.ID,
		FriendID: base64.StdEncoding.EncodeToString(
			Seeder.EncryptWithPublicKey(
				[]byte(u.ID.String()),
				decodedPublicKey,
			),
		),
		FriendUsername: base64.StdEncoding.EncodeToString(
			Seeder.EncryptWithPublicKey(
				[]byte(u.Username),
				decodedPublicKey,
			),
		),
		FriendPublicAsymmetricKey: base64.StdEncoding.EncodeToString(
			encPublicKey,
		),
		SymmetricKey: base64.StdEncoding.EncodeToString(
			Seeder.EncryptWithPublicKey(key.Key, decodedPublicKey),
		),
	}

	jsonStr, _ := json.Marshal([]Models.FriendRequest{friendReq, friendReq2})
	req, _ := http.NewRequest("POST", ts.URL+"/api/v1/auth/friend_request/qr_code", bytes.NewBuffer(jsonStr))
	req.Header.Set("Content-Type", "application/json")

	resp, err := client.Do(req)
	if err != nil {
		t.Errorf("Expected nil, recieved %s", err.Error())
		return
	}

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

	var r Models.FriendRequest

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

	if r.AcceptedAt.Valid == false {
		t.Errorf("Expected true, recieved false")
		return
	}
}
