package preproc

import (
	"os"
	"path/filepath"
	"strings"
	"testing"
)

func TestDiskFileReader_ReadLines_LibraryIncludes(t *testing.T) {
	origPath := os.Getenv("C65LIBPATH")
	defer func() { _ = os.Setenv("C65LIBPATH", origPath) }()

	libPath, _ := filepath.Abs("filereader_mocks/lib")
	_ = os.Setenv("C65LIBPATH", libPath)

	// Now create reader
	reader := NewDiskFileReader()

	tests := []struct {
		name        string
		includeSpec string
		wantErr     bool
	}{
		{
			name:        "simple library include",
			includeSpec: "<test_lib.c65>",
			wantErr:     false,
		},
		{
			name:        "nested library include",
			includeSpec: "<libsub/test_libsub.c65>",
			wantErr:     false,
		},
		{
			name:        "missing library file",
			includeSpec: "<notfound.c65>",
			wantErr:     true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			lines, _, err := reader.ReadLines(tt.includeSpec, "")
			if (err != nil) != tt.wantErr {
				t.Errorf("ReadLines() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if !tt.wantErr && len(lines) == 0 {
				t.Error("expected lines, got empty slice")
			}
		})
	}
}

func TestDiskFileReader_ReadLines_LibraryWithoutEnv(t *testing.T) {
	origPath := os.Getenv("C65LIBPATH")
	defer func() { _ = os.Setenv("C65LIBPATH", origPath) }()

	_ = os.Setenv("C65LIBPATH", "")

	// Now create reader
	reader := NewDiskFileReader()

	_, _, err := reader.ReadLines("<test_lib.c65>", "")
	if err == nil {
		t.Error("expected error when C65LIBPATH not set")
	}
}

func TestHybridFileReader_ReadLines_LibraryIncludes(t *testing.T) {
	origPath := os.Getenv("C65LIBPATH")
	defer func() { _ = os.Setenv("C65LIBPATH", origPath) }()

	// Test 1: With C65LIBPATH set, should use disk
	libPath, _ := filepath.Abs("filereader_mocks/lib")
	_ = os.Setenv("C65LIBPATH", libPath)
	
	reader1 := NewHybridFileReader()
	lines1, path1, err1 := reader1.ReadLines("<test_lib.c65>", "")
	if err1 != nil {
		t.Errorf("HybridFileReader with C65LIBPATH should work: %v", err1)
	}
	if len(lines1) == 0 {
		t.Error("expected lines from disk library")
	}
	if !strings.Contains(path1, "filereader_mocks") {
		t.Errorf("expected disk path, got: %s", path1)
	}

	// Test 2: Without C65LIBPATH, should use embedded (will fail in test since no embedded mock)
	_ = os.Setenv("C65LIBPATH", "")
	reader2 := NewHybridFileReader()
	_, _, err2 := reader2.ReadLines("<test_lib.c65>", "")
	if err2 == nil {
		// This is expected to fail in unit tests since we can't mock embedded FS easily
		// In real usage, embedded library would be used
		t.Log("Note: HybridFileReader without C65LIBPATH would use embedded library")
	}
}

func TestHybridFileReader_ReadLines_RelativeIncludes(t *testing.T) {
	reader := NewHybridFileReader()
	
	appDir, _ := filepath.Abs("filereader_mocks/app")
	
	lines, _, err := reader.ReadLines("test_app.c65", appDir)
	if err != nil {
		t.Errorf("HybridFileReader should handle relative includes: %v", err)
	}
	if len(lines) == 0 {
		t.Error("expected lines from relative include")
	}
}

func TestDiskFileReader_ReadLines_RelativeIncludes(t *testing.T) {
	reader := NewDiskFileReader()

	appDir, _ := filepath.Abs("filereader_mocks/app")

	tests := []struct {
		name        string
		includeSpec string
		currentDir  string
		wantErr     bool
	}{
		{
			name:        "bare include",
			includeSpec: "test_app.c65",
			currentDir:  appDir,
			wantErr:     false,
		},
		{
			name:        "quoted include",
			includeSpec: `"test_app.c65"`,
			currentDir:  appDir,
			wantErr:     false,
		},
		{
			name:        "single quoted include",
			includeSpec: "'test_app.c65'",
			currentDir:  appDir,
			wantErr:     false,
		},
		{
			name:        "nested relative include",
			includeSpec: "appsub/test_appsub.c65",
			currentDir:  appDir,
			wantErr:     false,
		},
		{
			name:        "missing file",
			includeSpec: "notfound.c65",
			currentDir:  appDir,
			wantErr:     true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			lines, _, err := reader.ReadLines(tt.includeSpec, tt.currentDir)
			if (err != nil) != tt.wantErr {
				t.Errorf("ReadLines() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if !tt.wantErr && len(lines) == 0 {
				t.Error("expected lines, got empty slice")
			}
		})
	}
}

func TestDiskFileReader_ReadLines_Cache(t *testing.T) {
	reader := NewDiskFileReader()

	appDir, _ := filepath.Abs("filereader_mocks/app")

	// First read
	lines1, _, err := reader.ReadLines("test_app.c65", appDir)
	if err != nil {
		t.Fatalf("first read failed: %v", err)
	}

	// Second read (should hit cache)
	lines2, _, err := reader.ReadLines("test_app.c65", appDir)
	if err != nil {
		t.Fatalf("second read failed: %v", err)
	}

	// Verify same slice (from cache)
	if len(lines1) != len(lines2) {
		t.Errorf("cache returned different length: %d vs %d", len(lines1), len(lines2))
	}

	// Verify cache was actually used (check map size)
	if len(reader.cache) != 1 {
		t.Errorf("expected 1 cache entry, got %d", len(reader.cache))
	}
}

func TestDiskFileReader_ReadLines_EmptyFile(t *testing.T) {
	reader := NewDiskFileReader()

	appDir, _ := filepath.Abs("filereader_mocks/app")

	lines, _, err := reader.ReadLines("empty.c65", appDir)
	if err != nil {
		t.Fatalf("failed to read empty file: %v", err)
	}

	// Empty file still returns slice with one empty string
	if len(lines) != 1 {
		t.Errorf("empty file: expected 1 line, got %d", len(lines))
	}
}

func TestDiskFileReader_ReadLines_EmptySpec(t *testing.T) {
	reader := NewDiskFileReader()

	_, _, err := reader.ReadLines("", "")
	if err == nil {
		t.Error("expected error for empty include spec")
	}
}

func TestMockFileReader_ReadLines(t *testing.T) {
	files := map[string][]string{
		"test.c65": {"LDA #$00", "STA $D020"},
		"empty":    {},
	}

	reader := NewMockFileReader(files)

	lines, _, err := reader.ReadLines("test.c65", "")
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if len(lines) != 2 {
		t.Errorf("expected 2 lines, got %d", len(lines))
	}

	_, _, err = reader.ReadLines("notfound", "")
	if err == nil {
		t.Error("expected error for missing file")
	}
}

func TestMockFileReader_LibraryInclude(t *testing.T) {
	files := map[string][]string{
		"<test_lib.c65>": {"LDA #$00", "STA $D020"},
	}

	reader := NewMockFileReader(files)

	lines, _, err := reader.ReadLines("<test_lib.c65>", "")
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if len(lines) != 2 {
		t.Errorf("expected 2 lines, got %d", len(lines))
	}
}

func TestMockFileReader_NormalInclude(t *testing.T) {
	files := map[string][]string{
		"test_app.c65": {"JMP $1000"},
	}

	reader := NewMockFileReader(files)

	lines, _, err := reader.ReadLines("test_app.c65", "some/dir")
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if len(lines) != 1 {
		t.Errorf("expected 1 line, got %d", len(lines))
	}
}

func TestMockFileReader_MissingFile(t *testing.T) {
	files := map[string][]string{
		"exists.c65": {"NOP"},
	}

	reader := NewMockFileReader(files)

	_, _, err := reader.ReadLines("notfound.c65", "")
	if err == nil {
		t.Error("expected error for missing file")
	}
}
