// Copyright 2026 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package integration_test

import (
	"context"
	"log"
	"testing"

	gitea "gitea.dev/sdk"

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

func TestMainNightlyIssueAssignees(t *testing.T) {
	log.Println("== TestIssueAssignees ==")
	c := newTestClient()

	repo, _ := createTestRepo(t, "RepoIssueAssignees", c)
	createTestUser(t, "ping", c)
	createTestUser(t, "pong", c)
	cleanupCtx := context.Background()
	defer func() {
		_, err := c.Admin.DeleteUser(cleanupCtx, "ping")
		assert.NoError(t, err)
		_, err = c.Admin.DeleteUser(cleanupCtx, "pong")
		assert.NoError(t, err)
		_, err = c.Repositories.DeleteRepo(cleanupCtx, repo.Owner.UserName, repo.Name)
		assert.NoError(t, err)
	}()

	noIssueExists, resp, err := c.Issues.AddIssueAssignees(t.Context(), repo.Owner.UserName, repo.Name, 0, gitea.IssueAssigneesOption{Assignees: []string{"ping"}})
	assert.Error(t, err)
	assert.EqualValues(t, 404, resp.StatusCode)
	assert.NotNil(t, noIssueExists)
	assert.EqualValues(t, 0, noIssueExists.Index)

	noIssueExists, resp, err = c.Issues.DeleteIssueAssignees(t.Context(), repo.Owner.UserName, repo.Name, 0, gitea.IssueAssigneesOption{Assignees: []string{"ping"}})
	assert.Error(t, err)
	assert.EqualValues(t, 404, resp.StatusCode)
	assert.NotNil(t, noIssueExists)
	assert.EqualValues(t, 0, noIssueExists.Index)

	issue, _, err := c.Issues.CreateIssue(t.Context(), repo.Owner.UserName, repo.Name, gitea.CreateIssueOption{Title: "Test Issue1"})
	assert.NoError(t, err)

	isAssignableNotExists, resp, err := c.Issues.CheckIssueAssignee(t.Context(), repo.Owner.UserName, repo.Name, "user_that_not_exists", issue.Index)
	assert.NoError(t, err)
	assert.EqualValues(t, 404, resp.StatusCode)
	assert.Equal(t, false, isAssignableNotExists)

	noUserExists, resp, err := c.Issues.AddIssueAssignees(t.Context(), repo.Owner.UserName, repo.Name, issue.Index, gitea.IssueAssigneesOption{Assignees: []string{"user_that_not_exists"}})
	assert.Error(t, err)
	assert.EqualValues(t, 422, resp.StatusCode)
	assert.NotNil(t, noUserExists)
	assert.EqualValues(t, 0, noUserExists.Index)

	noUserExists, resp, err = c.Issues.DeleteIssueAssignees(t.Context(), repo.Owner.UserName, repo.Name, issue.Index, gitea.IssueAssigneesOption{Assignees: []string{"user_that_not_exists"}})
	assert.Error(t, err)
	assert.EqualValues(t, 422, resp.StatusCode)
	assert.NotNil(t, noUserExists)
	assert.EqualValues(t, 0, noUserExists.Index)

	isAssignablePing, resp, err := c.Issues.CheckIssueAssignee(t.Context(), repo.Owner.UserName, repo.Name, "ping", issue.Index)
	assert.NoError(t, err)
	assert.EqualValues(t, 204, resp.StatusCode)
	assert.Equal(t, true, isAssignablePing)

	issue, resp, err = c.Issues.AddIssueAssignees(t.Context(), repo.Owner.UserName, repo.Name, issue.Index, gitea.IssueAssigneesOption{Assignees: []string{"ping", "pong"}})
	assert.NoError(t, err)
	assert.EqualValues(t, 201, resp.StatusCode)
	assert.EqualValues(t, 2, len(issue.Assignees))

	issue, resp, err = c.Issues.DeleteIssueAssignees(t.Context(), repo.Owner.UserName, repo.Name, issue.Index, gitea.IssueAssigneesOption{Assignees: []string{"ping", "pong"}})
	assert.NoError(t, err)
	assert.EqualValues(t, 200, resp.StatusCode)
	assert.EqualValues(t, 0, len(issue.Assignees))
}
