package peer_test

import (
	"testing"

	"cloud.o-forge.io/core/oc-lib/models/peer"
	"cloud.o-forge.io/core/oc-lib/tools"
	"github.com/stretchr/testify/assert"
)

// ---- PeerRelation ----

func TestPeerRelation_String(t *testing.T) {
	assert.Equal(t, "UNKNOWN", peer.NONE.String())
	assert.Equal(t, "SELF", peer.SELF.String())
	assert.Equal(t, "PARTNER", peer.PARTNER.String())
	assert.Equal(t, "BLACKLIST", peer.BLACKLIST.String())
}

func TestPeerRelation_Path(t *testing.T) {
	assert.Equal(t, "unknown", peer.NONE.Path())
	assert.Equal(t, "self", peer.SELF.Path())
	assert.Equal(t, "partner", peer.PARTNER.Path())
	assert.Equal(t, "blacklist", peer.BLACKLIST.Path())
}

func TestPeerRelation_EnumIndex(t *testing.T) {
	assert.Equal(t, 0, peer.NONE.EnumIndex())
	assert.Equal(t, 1, peer.SELF.EnumIndex())
	assert.Equal(t, 2, peer.PARTNER.EnumIndex())
	assert.Equal(t, 3, peer.BLACKLIST.EnumIndex())
	assert.Equal(t, 4, peer.PENDING_PARTNER.EnumIndex())
}

func TestGetRelationPath(t *testing.T) {
	assert.Equal(t, 1, peer.GetRelationPath("self"))
	assert.Equal(t, 2, peer.GetRelationPath("partner"))
	assert.Equal(t, 3, peer.GetRelationPath("blacklist"))
	assert.Equal(t, -1, peer.GetRelationPath("nonexistent"))
}

// ---- Peer model ----

func TestPeer_VerifyAuth(t *testing.T) {
	p := &peer.Peer{}
	assert.True(t, p.VerifyAuth("get", nil))
	assert.True(t, p.VerifyAuth("delete", &tools.APIRequest{}))
}

func TestPeer_CanDelete(t *testing.T) {
	p := &peer.Peer{}
	assert.False(t, p.CanDelete())
}

func TestPeer_GetAccessor(t *testing.T) {
	p := &peer.Peer{}
	req := &tools.APIRequest{}
	acc := p.GetAccessor(req)
	assert.NotNil(t, acc)
}

func TestPeer_AddExecution_Deduplication(t *testing.T) {
	p := &peer.Peer{}
	exec := peer.PeerExecution{Method: "POST", Url: "http://peer/data", Body: "body1"}

	p.AddExecution(exec)
	assert.Len(t, p.FailedExecution, 1)

	// Second add of same execution should not duplicate
	p.AddExecution(exec)
	assert.Len(t, p.FailedExecution, 1)

	// Different execution should be added
	exec2 := peer.PeerExecution{Method: "GET", Url: "http://peer/data", Body: nil}
	p.AddExecution(exec2)
	assert.Len(t, p.FailedExecution, 2)
}

func TestPeer_RemoveExecution(t *testing.T) {
	p := &peer.Peer{}
	exec1 := peer.PeerExecution{Method: "POST", Url: "http://peer/a", Body: nil}
	exec2 := peer.PeerExecution{Method: "DELETE", Url: "http://peer/b", Body: nil}

	p.AddExecution(exec1)
	p.AddExecution(exec2)
	assert.Len(t, p.FailedExecution, 2)

	p.RemoveExecution(exec1)
	assert.Len(t, p.FailedExecution, 1)
	assert.Equal(t, exec2, p.FailedExecution[0])
}

func TestPeer_RemoveExecution_NotFound(t *testing.T) {
	p := &peer.Peer{}
	exec := peer.PeerExecution{Method: "POST", Url: "http://peer/x", Body: nil}
	p.AddExecution(exec)

	other := peer.PeerExecution{Method: "DELETE", Url: "http://other/x", Body: nil}
	p.RemoveExecution(other)
	assert.Len(t, p.FailedExecution, 1) // unchanged
}

func TestPeer_RemoveExecution_Empty(t *testing.T) {
	p := &peer.Peer{}
	// Should not panic on empty list
	exec := peer.PeerExecution{Method: "GET", Url: "http://peer/x", Body: nil}
	p.RemoveExecution(exec)
	assert.Empty(t, p.FailedExecution)
}
