package editor

import (
	"testing"

	"git.gophernest.net/azpect/TextEditor/internal/action"
)

// TestHelperExamples demonstrates the different ways to use the test helpers
func TestHelperExamples(t *testing.T) {
	t.Run("basic model with defaults", func(t *testing.T) {
		// Uses default: 6 lines, cursor at 0,0, terminal 80x24
		tm := newTestModel(t)
		_ = getFinalModel(t, tm)
	})

	t.Run("custom lines only", func(t *testing.T) {
		tm := newTestModel(t,
			WithLines([]string{"hello", "world"}),
		)
		m := getFinalModel(t, tm)
		if len(m.Lines()) != 2 {
			t.Errorf("expected 2 lines, got %d", len(m.Lines()))
		}
	})

	t.Run("custom cursor position", func(t *testing.T) {
		tm := newTestModel(t,
			WithCursorPos(action.Position{Line: 2, Col: 3}),
		)
		m := getFinalModel(t, tm)
		if m.CursorY() != 2 || m.CursorX() != 3 {
			t.Errorf("expected cursor at (2,3), got (%d,%d)", m.CursorY(), m.CursorX())
		}
	})

	t.Run("custom terminal size", func(t *testing.T) {
		tm := newTestModel(t,
			WithTermSize(120, 40),
		)
		m := getFinalModel(t, tm)
		if m.WinW() != 120 || m.WinH() != 40 {
			t.Errorf("expected size 120x40, got %dx%d", m.WinW(), m.WinH())
		}
	})

	t.Run("with register content for paste testing", func(t *testing.T) {
		tm := newTestModel(t,
			WithLines([]string{"hello world"}),
			WithRegister('"', action.CharwiseRegister, []string{"foo"}),
		)
		m := getFinalModel(t, tm)

		reg, ok := m.GetRegister('"')
		if !ok {
			t.Fatal("expected register to be set")
		}
		if reg.Type != action.CharwiseRegister {
			t.Errorf("expected charwise register, got %v", reg.Type)
		}
		if len(reg.Content) != 1 || reg.Content[0] != "foo" {
			t.Errorf("expected content ['foo'], got %v", reg.Content)
		}
	})

	t.Run("combine multiple options", func(t *testing.T) {
		tm := newTestModel(t,
			WithLines([]string{"line one", "line two", "line three"}),
			WithCursorPos(action.Position{Line: 1, Col: 5}),
			WithTermSize(100, 30),
			WithRegister('"', action.LinewiseRegister, []string{"deleted line 1", "deleted line 2"}),
		)
		m := getFinalModel(t, tm)

		// Verify all options were applied
		if len(m.Lines()) != 3 {
			t.Errorf("expected 3 lines, got %d", len(m.Lines()))
		}
		if m.CursorY() != 1 || m.CursorX() != 5 {
			t.Errorf("expected cursor at (1,5), got (%d,%d)", m.CursorY(), m.CursorX())
		}
		if m.WinW() != 100 || m.WinH() != 30 {
			t.Errorf("expected size 100x30, got %dx%d", m.WinW(), m.WinH())
		}

		reg, ok := m.GetRegister('"')
		if !ok || reg.Type != action.LinewiseRegister {
			t.Error("register not set correctly")
		}
	})

	t.Run("backward compatible helpers still work", func(t *testing.T) {
		// Old style helpers still work for existing tests
		tm1 := newTestModelWithLines(t, []string{"a", "b"})
		m1 := getFinalModel(t, tm1)
		if len(m1.Lines()) != 2 {
			t.Error("newTestModelWithLines failed")
		}

		tm2 := newTestModelWithCursorPos(t, action.Position{Line: 1, Col: 2})
		m2 := getFinalModel(t, tm2)
		if m2.CursorY() != 1 {
			t.Error("newTestModelWithCursorPos failed")
		}

		tm3 := newTestModelWithLinesAndCursorPos(t, []string{"x"}, action.Position{Line: 0, Col: 0})
		m3 := getFinalModel(t, tm3)
		if len(m3.Lines()) != 1 {
			t.Error("newTestModelWithLinesAndCursorPos failed")
		}

		tm4 := newTestModelWithTermSize(t, []string{"y"}, action.Position{Line: 0, Col: 0}, 50, 20)
		m4 := getFinalModel(t, tm4)
		if m4.WinW() != 50 {
			t.Error("newTestModelWithTermSize failed")
		}
	})
}
