package models

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestAccountFilter(t *testing.T) {
	accounts := []DataPlaneAccountInfo{
		{
			AWSAccountInfo: &AWSAccountInfo{
				AWSAccount:  "account1",
				AWSRole:     "role1",
				CMEKEnabled: true,
			},
		},
		{
			AWSAccountInfo: &AWSAccountInfo{
				AWSAccount: "account2",
				AWSRole:    "role2",
			},
		},
		{
			AWSAccountInfo: &AWSAccountInfo{
				AWSAccount: "account3",
				AWSRole:    "role3",
			},
		},
		{
			AWSAccountInfo: &AWSAccountInfo{
				AWSAccount: "account4",
				AWSRole:    "role4",
			},
		},
		{
			GCPAccountInfo: &GCPAccountInfo{
				ProjectID:   "project1",
				CMEKEnabled: true,
			},
		},
		{
			GCPAccountInfo: &GCPAccountInfo{
				ProjectID: "project1",
			},
		},
		{
			AzureAccountInfo: &AzureAccountInfo{
				SubscriptionID: "subscriotion-01",
				CMEKEnabled:    true,
			},
		},
		{
			AzureAccountInfo: &AzureAccountInfo{
				SubscriptionID: "subscriotion-02",
			},
		},
	}
	filter := AccountFilter{
		CMEKEnabled: true,
	}
	filteredAccounts := filter.Apply(accounts)
	assert.Equal(t, 3, len(filteredAccounts))
	filter.CMEKEnabled = false
	filteredAccounts = filter.Apply(accounts)
	assert.Equal(t, 5, len(filteredAccounts))
}

func TestDataPlaneAccountInfo(t *testing.T) {
	t.Run("GetKey", func(t *testing.T) {
		assert.Equal(t, "account1", DataPlaneAccountInfo{
			AWSAccountInfo: &AWSAccountInfo{
				AWSAccount: "account1",
				AWSRole:    "role1",
			},
		}.GetKey())
		assert.Equal(t, "project1", DataPlaneAccountInfo{
			GCPAccountInfo: &GCPAccountInfo{
				ProjectID: "project1",
			},
		}.GetKey())
		assert.Equal(t, "", DataPlaneAccountInfo{}.GetKey())
		assert.Equal(t, "subscription-01", DataPlaneAccountInfo{
			AzureAccountInfo: &AzureAccountInfo{
				SubscriptionID: "subscription-01",
			},
		}.GetKey())
	})
	t.Run("Is", func(t *testing.T) {
		assert.True(t, DataPlaneAccountInfo{
			AWSAccountInfo: &AWSAccountInfo{
				AWSAccount: "account1",
				AWSRole:    "role1",
			},
		}.Is("account1"))
		assert.False(t, DataPlaneAccountInfo{
			AWSAccountInfo: &AWSAccountInfo{
				AWSAccount: "account1",
				AWSRole:    "role1",
			},
		}.Is("account2"))
		assert.True(t, DataPlaneAccountInfo{
			GCPAccountInfo: &GCPAccountInfo{
				ProjectID: "project1",
			},
		}.Is("project1"))
		assert.False(t, DataPlaneAccountInfo{
			GCPAccountInfo: &GCPAccountInfo{
				ProjectID: "project1",
			},
		}.Is("project2"))
		assert.False(t, DataPlaneAccountInfo{}.Is("project2"))
		assert.True(t, DataPlaneAccountInfo{
			AzureAccountInfo: &AzureAccountInfo{
				SubscriptionID: "subscription-01",
			},
		}.Is("subscription-01"))
		assert.False(t, DataPlaneAccountInfo{
			AzureAccountInfo: &AzureAccountInfo{
				SubscriptionID: "subscription-01",
			},
		}.Is("subscription-02"))
	})
}

func TestGetAccountID(t *testing.T) {
	t.Run("GetAccountID", func(t *testing.T) {
		assert.Equal(t, "account1", DataPlaneAccountInfo{
			AWSAccountInfo: &AWSAccountInfo{
				AWSAccount: "account1",
				AWSRole:    "role1",
			},
		}.GetAccountID())
		assert.Equal(t, "project1", DataPlaneAccountInfo{
			GCPAccountInfo: &GCPAccountInfo{
				ProjectID: "project1",
			},
		}.GetAccountID())
		assert.Equal(t, "subscription-01", DataPlaneAccountInfo{
			AzureAccountInfo: &AzureAccountInfo{
				SubscriptionID: "subscription-01",
			},
		}.GetAccountID())
		assert.Equal(t, "", DataPlaneAccountInfo{}.GetAccountID())
	})
}
