package mcp

import (
	"testing"

	"github.com/google/uuid"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestCreateProject(t *testing.T) {
	deps := testDeps()

	result, err := callTool(ctxWithAuth(Scope{}), deps, "create_project", map[string]any{
		"slug": "new-proj", "name": "New Project", "visibility": "internal",
	})
	require.NoError(t, err)
	require.False(t, result.IsError)

	var out projectCreateOut
	unmarshalStructured(t, result, &out)
	assert.True(t, out.OK)
	assert.Equal(t, "new-proj", out.Project.Slug)
	assert.Equal(t, "internal", out.Project.Visibility)
}

func TestCreateProject_DeniedForProjectScopedToken(t *testing.T) {
	deps := testDeps()

	// token 限定到具体 project 列表 → 不允许创建新项目
	result, err := callTool(ctxWithAuth(Scope{ProjectIDs: []uuid.UUID{testPID}}), deps,
		"create_project", map[string]any{"slug": "x", "name": "X"})
	require.NoError(t, err)
	assert.True(t, result.IsError)
}

func TestUpdateProject(t *testing.T) {
	deps := testDeps()

	result, err := callTool(ctxWithAuth(Scope{}), deps, "update_project", map[string]any{
		"project_slug": "test-project", "name": "Renamed", "description": "new desc",
	})
	require.NoError(t, err)
	require.False(t, result.IsError)

	var out projectCreateOut
	unmarshalStructured(t, result, &out)
	assert.Equal(t, "Renamed", out.Project.Name)
	assert.Equal(t, "new desc", out.Project.Description)
}

func TestArchiveUnarchiveProject(t *testing.T) {
	deps := testDeps()

	result, err := callTool(ctxWithAuth(Scope{}), deps, "archive_project", map[string]any{
		"project_slug": "test-project",
	})
	require.NoError(t, err)
	var out okOut
	unmarshalStructured(t, result, &out)
	assert.True(t, out.OK)

	result, err = callTool(ctxWithAuth(Scope{}), deps, "unarchive_project", map[string]any{
		"project_slug": "test-project",
	})
	require.NoError(t, err)
	unmarshalStructured(t, result, &out)
	assert.True(t, out.OK)
}
