package controllers

import (
	"testing"
)

func TestExtractEmailSimple(t *testing.T) {
	tests := []struct {
		input string
		want  string
	}{
		{"user@example.com", "user@example.com"},
		{"<user@example.com>", "user@example.com"},
		{"John Doe <john@example.com>", "john@example.com"},
		{"\"John Doe\" <john@example.com>", "john@example.com"},
		{"  spaced@example.com  ", "spaced@example.com"},
		{"", ""},
		{"<onlybrackets>", "onlybrackets"},
	}
	for _, tt := range tests {
		got := extractEmail(tt.input)
		if got != tt.want {
			t.Errorf("extractEmail(%q) = %q, want %q", tt.input, got, tt.want)
		}
	}
}

func TestExtractNameSimple(t *testing.T) {
	tests := []struct {
		input string
		want  string
	}{
		{"John Doe <john@example.com>", "John Doe"},
		{"<user@example.com>", ""},
		{"user@example.com", ""},
		{"  Spaces Here <spaces@example.com>", "Spaces Here"},
		{"\"Quoted Name\" <q@example.com>", "\"Quoted Name\""},
		{"", ""},
	}
	for _, tt := range tests {
		got := extractName(tt.input)
		if got != tt.want {
			t.Errorf("extractName(%q) = %q, want %q", tt.input, got, tt.want)
		}
	}
}

func TestExtractEmailRealWorld(t *testing.T) {
	inputs := []struct {
		full string
		mail string
		name string
	}{
		{"María López <maria@example.com>", "maria@example.com", "María López"},
		{"soporte@u-s.app", "soporte@u-s.app", ""},
		{"Cliente Final <cliente+tag@dominio.co>", "cliente+tag@dominio.co", "Cliente Final"},
		{"", "", ""},
	}
	for _, tt := range inputs {
		gotMail := extractEmail(tt.full)
		gotName := extractName(tt.full)
		if gotMail != tt.mail {
			t.Errorf("extractEmail(%q) = %q, want %q", tt.full, gotMail, tt.mail)
		}
		if gotName != tt.name {
			t.Errorf("extractName(%q) = %q, want %q", tt.full, gotName, tt.name)
		}
	}
}

func TestExtractEmailEdgeCases(t *testing.T) {
	// Formato RFC 5322 con nombre y ángulos
	if got := extractEmail("a<b@c.com>"); got != "b@c.com" {
		t.Errorf("extractEmail('a<b@c.com>') = %q, want 'b@c.com'", got)
	}
	// Múltiples brackets — usa el último par
	if got := extractEmail("<a><b@c.com>"); got != "b@c.com" {
		t.Errorf("extractEmail('<a><b@c.com>') = %q, want 'b@c.com'", got)
	}
}
