package external

import (
	"testing"

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

func TestImport(t *testing.T) {
	importer := NewTaskImporter()
	
	task := &ExternalTask{
		ID:         "github-123",
		Source:     "github",
		ExternalID: "123",
		Title:      "Add feature",
	}
	
	err := importer.Import(task)
	assert.NoError(t, err)
}

func TestGetTask(t *testing.T) {
	importer := NewTaskImporter()
	
	task := &ExternalTask{
		ID:         "github-123",
		Source:     "github",
		ExternalID: "123",
		Title:      "Add feature",
	}
	
	importer.Import(task)
	
	retrieved, found := importer.GetTask("github-123")
	assert.True(t, found)
	assert.Equal(t, "Add feature", retrieved.Title)
}

func TestListTasks(t *testing.T) {
	importer := NewTaskImporter()
	
	for i := 0; i < 3; i++ {
		importer.Import(&ExternalTask{
			ID:     "task-" + string(rune(48+i)),
			Source: "github",
		})
	}
	
	tasks := importer.ListTasks()
	assert.Equal(t, 3, len(tasks))
}

func TestUpdateStatus(t *testing.T) {
	importer := NewTaskImporter()
	
	task := &ExternalTask{
		ID:     "github-123",
		Source: "github",
		Status: "open",
	}
	
	importer.Import(task)
	importer.UpdateStatus("github-123", "closed")
	
	updated, _ := importer.GetTask("github-123")
	assert.Equal(t, "closed", updated.Status)
}

func TestGetBySource(t *testing.T) {
	importer := NewTaskImporter()
	
	importer.Import(&ExternalTask{ID: "gh-1", Source: "github"})
	importer.Import(&ExternalTask{ID: "gh-2", Source: "github"})
	importer.Import(&ExternalTask{ID: "jira-1", Source: "jira"})
	
	github := importer.GetBySource("github")
	assert.Equal(t, 2, len(github))
}
