package core

import (
	"log"
	"testing"
)

func TestTestCaptureLogs(t *testing.T) {
	t.Run("log.Print capturado en el buffer", func(t *testing.T) {
		buf, _ := TestCaptureLogs(t)

		log.Print("hello from test")

		if buf.Len() == 0 {
			t.Error("expected buffer to have content, got empty")
		}

		content := buf.String()
		if content == "" {
			t.Error("expected non-empty buffer content")
		}
	})

	t.Run("flush retorna lineas separadas", func(t *testing.T) {
		_, flush := TestCaptureLogs(t)

		log.Print("line one")
		log.Print("line two")
		log.Print("line three")

		lines := flush()
		if len(lines) != 3 {
			t.Errorf("got %d lines, want 3: %v", len(lines), lines)
		}
		if lines[0] != "line one" {
			t.Errorf("line[0] = %q, want \"line one\"", lines[0])
		}
		if lines[1] != "line two" {
			t.Errorf("line[1] = %q, want \"line two\"", lines[1])
		}
		if lines[2] != "line three" {
			t.Errorf("line[2] = %q, want \"line three\"", lines[2])
		}
	})

	t.Run("sin logs flush retorna nil", func(t *testing.T) {
		_, flush := TestCaptureLogs(t)
		lines := flush()
		if lines != nil {
			t.Errorf("expected nil for no logs, got %v", lines)
		}
	})

	t.Run("log restaurado al original tras flush", func(t *testing.T) {
		_, flush := TestCaptureLogs(t)
		log.Print("captured")
		flush()

		// Despues de flush el logger esta restaurado
		// Verificamos que no panics ni errores al loggear normalmente
		// (el output va al writer original, no al buffer)
		log.Print("this goes to original output")
	})
}
