package testkit

import (
	"net/http"
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/tidbcloud/AgentTeam/internal/api/dto"
	"github.com/tidbcloud/AgentTeam/internal/store/organization"
)

// TestFeishuCredentialEncryptionKey returns the deterministic key used by org/auth integration tests.
func TestFeishuCredentialEncryptionKey() []byte {
	return []byte("0123456789abcdef0123456789abcdef")
}

// PutOrganizationFeishuCredentialsForTests stores Feishu credentials through the org HTTP API.
func PutOrganizationFeishuCredentialsForTests(t *testing.T, client *http.Client, baseURL string, accessToken string, organizationID string, appID string, appSecret string) organization.OrganizationEnterpriseFeishuView {
	t.Helper()

	var enterprise organization.OrganizationEnterpriseFeishuView
	response := DoJSON(t, client, http.MethodPut, baseURL+"/api/v1/orgs/"+organizationID+"/enterprise/feishu/credentials", map[string]any{
		"appId":     appID,
		"appSecret": appSecret,
	}, AuthHeaders(accessToken), &enterprise)
	require.Equal(t, http.StatusOK, response.StatusCode)
	return enterprise
}

// OrganizationIDsFromSummaries extracts IDs from organization summaries.
func OrganizationIDsFromSummaries(organizations []organization.OrganizationSummary) []string {
	ids := make([]string, 0, len(organizations))
	for _, organization := range organizations {
		ids = append(ids, organization.ID)
	}
	return ids
}

// ListOrganizationsForTests lists organizations visible to a test user.
func ListOrganizationsForTests(t *testing.T, client *http.Client, baseURL string, accessToken string) []organization.OrganizationSummary {
	t.Helper()

	var organizations []organization.OrganizationSummary
	response := DoJSON(t, client, http.MethodGet, baseURL+"/api/v1/orgs", nil, AuthHeaders(accessToken), &organizations)
	require.Equal(t, http.StatusOK, response.StatusCode)
	return organizations
}

// FirstOrganizationForTests returns the first organization visible to a test user.
func FirstOrganizationForTests(t *testing.T, client *http.Client, baseURL string, accessToken string) organization.OrganizationSummary {
	t.Helper()

	organizations := ListOrganizationsForTests(t, client, baseURL, accessToken)
	require.NotEmpty(t, organizations)
	return organizations[0]
}

// FirstOrganizationIDForTests returns the first organization visible to a test user.
func FirstOrganizationIDForTests(t *testing.T, client *http.Client, baseURL string, accessToken string) string {
	t.Helper()

	return FirstOrganizationForTests(t, client, baseURL, accessToken).ID
}

// CreateWorkspaceForOrganizationTests creates a workspace through the explicit organization ownership API contract.
func CreateWorkspaceForOrganizationTests(t *testing.T, client *http.Client, baseURL string, accessToken string, organizationID string, name string, slug string) dto.WorkspaceResponse {
	t.Helper()

	var workspace dto.WorkspaceResponse
	response := DoJSON(t, client, http.MethodPost, baseURL+"/api/v1/workspaces", map[string]any{
		"organizationId": organizationID,
		"name":           name,
		"slug":           slug,
	}, AuthHeaders(accessToken), &workspace)
	require.Equal(t, http.StatusOK, response.StatusCode)
	return workspace
}
