package postgres

import (
	"testing"

	"github.com/orchard9/rdev/internal/domain"
)

// Helper function conversion tests

func TestScopesToStrings(t *testing.T) {
	scopes := []domain.Scope{domain.ScopeProjectsRead, domain.ScopeAdmin}
	strings := scopesToStrings(scopes)

	if len(strings) != 2 {
		t.Fatalf("Length = %d, want 2", len(strings))
	}
	if strings[0] != "projects:read" {
		t.Errorf("strings[0] = %q, want %q", strings[0], "projects:read")
	}
	if strings[1] != "admin" {
		t.Errorf("strings[1] = %q, want %q", strings[1], "admin")
	}
}

func TestScopesFromStrings(t *testing.T) {
	strings := []string{"projects:read", "keys:write"}
	scopes := scopesFromStrings(strings)

	if len(scopes) != 2 {
		t.Fatalf("Length = %d, want 2", len(scopes))
	}
	if scopes[0] != domain.ScopeProjectsRead {
		t.Errorf("scopes[0] = %q, want %q", scopes[0], domain.ScopeProjectsRead)
	}
	if scopes[1] != domain.ScopeKeysWrite {
		t.Errorf("scopes[1] = %q, want %q", scopes[1], domain.ScopeKeysWrite)
	}
}

func TestProjectIDsToStrings(t *testing.T) {
	t.Run("nil input", func(t *testing.T) {
		result := projectIDsToStrings(nil)
		if result != nil {
			t.Errorf("Expected nil, got %v", result)
		}
	})

	t.Run("non-nil input", func(t *testing.T) {
		ids := []domain.ProjectID{"proj-a", "proj-b"}
		result := projectIDsToStrings(ids)
		if len(result) != 2 {
			t.Fatalf("Length = %d, want 2", len(result))
		}
		if result[0] != "proj-a" || result[1] != "proj-b" {
			t.Errorf("Unexpected result: %v", result)
		}
	})
}

func TestProjectIDsFromStrings(t *testing.T) {
	t.Run("nil input", func(t *testing.T) {
		result := projectIDsFromStrings(nil)
		if result != nil {
			t.Errorf("Expected nil, got %v", result)
		}
	})

	t.Run("non-nil input", func(t *testing.T) {
		strings := []string{"proj-x", "proj-y"}
		result := projectIDsFromStrings(strings)
		if len(result) != 2 {
			t.Fatalf("Length = %d, want 2", len(result))
		}
		if result[0] != "proj-x" || result[1] != "proj-y" {
			t.Errorf("Unexpected result: %v", result)
		}
	})
}
