package auth

import (
	"testing"

	"secondbit.org/uuid"
)

var clientStores = []ClientStore{NewMemstore()}

func TestClientStoreSuccess(t *testing.T) {
	client := Client{
		ID:          uuid.NewID(),
		Secret:      "secret",
		RedirectURI: "redirectURI",
		OwnerID:     uuid.NewID(),
		Name:        "name",
		Logo:        "logo",
		Website:     "website",
	}
	for _, store := range clientStores {
		err := store.SaveClient(client)
		if err != nil {
			t.Errorf("Error saving client to %T: %s", store, err)
		}
		retrieved, err := store.GetClient(client.ID)
		if err != nil {
			t.Errorf("Error retrieving client from %T: %s", store, err)
		}
		t.Log(retrieved)
		// TODO: compare retrieved to client
		clients, err := store.ListClientsByOwner(client.OwnerID, 25, 0)
		if err != nil {
			t.Errorf("Error retrieving clients by owner from %T: %s", store, err)
		}
		if len(clients) != 1 {
			t.Errorf("Expected 1 client in response from %T, got %+v", store, clients)
		}
		// TODO: compare clients[0] to client
		err = store.DeleteClient(client.ID)
		if err != nil {
			t.Errorf("Error deleting client from %T: %s", store, err)
		}
		retrieved, err = store.GetClient(client.ID)
		if err != ErrClientNotFound {
			t.Errorf("Expected ErrClientNotFound from %T, got %+v and %s", store, retrieved, err)
		}
		clients, err = store.ListClientsByOwner(client.OwnerID, 25, 0)
		if err != nil {
			t.Errorf("Error listing clients by owner from %T: %s", store, err)
		}
		if len(clients) != 0 {
			t.Errorf("Expected 0 clients in response from %T, got %+v", store, clients)
		}
	}
}
