package git

import (
	"os"
	"testing"
)

func TestParseDiff_SimpleAddition(t *testing.T) {
	// Read the example diff file
	content, err := os.ReadFile("testdata/diff1.txt")
	if err != nil {
		t.Fatalf("Failed to read test file: %v", err)
	}

	result, err := parseDiff(string(content))
	if err != nil {
		t.Fatalf("parseDiff failed: %v", err)
	}

	// diff1.txt has one addition: print("hello") at line 71
	if len(result.Changes) != 1 {
		t.Fatalf("Expected 1 change, got %d", len(result.Changes))
	}

	change := result.Changes[0]
	if change.File != "airflow/www/app.py" {
		t.Errorf("Expected file 'airflow/www/app.py', got %q", change.File)
	}
	if change.LineNumber != 71 {
		t.Errorf("Expected line number 71, got %d", change.LineNumber)
	}
	if change.Content != "    print(\"hello\")" {
		t.Errorf("Expected content '    print(\"hello\")', got %q", change.Content)
	}
	if change.ChangeType != "+" {
		t.Errorf("Expected change type '+', got %q", change.ChangeType)
	}
}

func TestParseDiff_ModificationWithContext(t *testing.T) {
	// Read the example diff file
	content, err := os.ReadFile("testdata/diff-with-context.txt")
	if err != nil {
		t.Fatalf("Failed to read test file: %v", err)
	}

	result, err := parseDiff(string(content))
	if err != nil {
		t.Fatalf("parseDiff failed: %v", err)
	}

	// diff-with-context.txt has one line changed (old_line -> new_line)
	// We only track additions, not deletions
	if len(result.Changes) != 1 {
		t.Fatalf("Expected 1 change (the addition), got %d", len(result.Changes))
	}

	change := result.Changes[0]
	if change.File != "example/app.py" {
		t.Errorf("Expected file 'example/app.py', got %q", change.File)
	}
	if change.Content != "    new_line = \"new_value\"" {
		t.Errorf("Expected 'new_line' content, got %q", change.Content)
	}
	if change.ChangeType != "+" {
		t.Errorf("Expected change type '+', got %q", change.ChangeType)
	}
}

func TestParseDiff_AdditionWithContext(t *testing.T) {
	// Read the example diff file
	content, err := os.ReadFile("testdata/diff-addition-with-context.txt")
	if err != nil {
		t.Fatalf("Failed to read test file: %v", err)
	}

	result, err := parseDiff(string(content))
	if err != nil {
		t.Fatalf("parseDiff failed: %v", err)
	}

	// diff-addition-with-context.txt has one line added
	if len(result.Changes) != 1 {
		t.Fatalf("Expected 1 change, got %d", len(result.Changes))
	}

	change := result.Changes[0]
	if change.File != "example/addition.py" {
		t.Errorf("Expected file 'example/addition.py', got %q", change.File)
	}
	if change.Content != "    new_functionality = True" {
		t.Errorf("Expected 'new_functionality = True', got %q", change.Content)
	}
	if change.ChangeType != "+" {
		t.Errorf("Expected change type '+', got %q", change.ChangeType)
	}
}

func TestParseDiff_EmptyDiff(t *testing.T) {
	result, err := parseDiff("")
	if err != nil {
		t.Fatalf("parseDiff failed: %v", err)
	}

	if len(result.Changes) != 0 {
		t.Errorf("Expected 0 changes for empty diff, got %d", len(result.Changes))
	}
}

func TestParseDiff_MultipleFiles(t *testing.T) {
	diffText := `diff --git a/file1.py b/file1.py
index 1234567..abcdefg 100644
--- a/file1.py
+++ b/file1.py
@@ -10,0 +11 @@ def main():
+    print("file1")
diff --git a/file2.py b/file2.py
index 7654321..gfedcba 100644
--- a/file2.py
+++ b/file2.py
@@ -20,0 +21 @@ def test():
+    print("file2")
`

	result, err := parseDiff(diffText)
	if err != nil {
		t.Fatalf("parseDiff failed: %v", err)
	}

	if len(result.Changes) != 2 {
		t.Fatalf("Expected 2 changes, got %d", len(result.Changes))
	}

	// Check first file
	if result.Changes[0].File != "file1.py" {
		t.Errorf("Expected file 'file1.py', got %q", result.Changes[0].File)
	}
	if result.Changes[0].LineNumber != 11 {
		t.Errorf("Expected line 11, got %d", result.Changes[0].LineNumber)
	}

	// Check second file
	if result.Changes[1].File != "file2.py" {
		t.Errorf("Expected file 'file2.py', got %q", result.Changes[1].File)
	}
	if result.Changes[1].LineNumber != 21 {
		t.Errorf("Expected line 21, got %d", result.Changes[1].LineNumber)
	}
}

func TestParseDiff_IgnoresDeletions(t *testing.T) {
	diffText := `diff --git a/test.py b/test.py
index 1234567..abcdefg 100644
--- a/test.py
+++ b/test.py
@@ -5,2 +5,1 @@ def func():
-    old_line_1 = "removed"
-    old_line_2 = "also removed"
+    new_line = "added"
`

	result, err := parseDiff(diffText)
	if err != nil {
		t.Fatalf("parseDiff failed: %v", err)
	}

	// Should only track the addition, not the 2 deletions
	if len(result.Changes) != 1 {
		t.Fatalf("Expected 1 change (addition only), got %d", len(result.Changes))
	}

	if result.Changes[0].Content != "    new_line = \"added\"" {
		t.Errorf("Expected addition content, got %q", result.Changes[0].Content)
	}
}
