package migration

import (
	"encoding/json"
	"testing"
)

func TestGmailUIDStable(t *testing.T) {
	a := gmailUID("18c4f2a1b2d3e4f5")
	b := gmailUID("18c4f2a1b2d3e4f5")
	if a != b {
		t.Fatalf("expected stable uid, got %d vs %d", a, b)
	}
	if a <= 0 {
		t.Fatalf("expected positive uid, got %d", a)
	}
}

func TestPrimaryGmailFolder(t *testing.T) {
	remote, folderType := primaryGmailFolder([]string{"INBOX", "UNREAD"})
	if remote != "INBOX" || folderType != "inbox" {
		t.Fatalf("got %q / %q", remote, folderType)
	}

	remote, folderType = primaryGmailFolder([]string{"SENT"})
	if remote != "SENT" || folderType != "sent" {
		t.Fatalf("got %q / %q", remote, folderType)
	}

	remote, folderType = primaryGmailFolder([]string{"Label_123", "INBOX"})
	if remote != "INBOX" || folderType != "inbox" {
		t.Fatalf("inbox wins over label: got %q / %q", remote, folderType)
	}
}

func TestParseAddressListJSON(t *testing.T) {
	raw := parseAddressListJSON(`Alice <alice@example.com>, bob@example.com`)
	var addrs []map[string]string
	if err := json.Unmarshal(raw, &addrs); err != nil {
		t.Fatal(err)
	}
	if len(addrs) != 2 {
		t.Fatalf("expected 2 addresses, got %d", len(addrs))
	}
	if addrs[0]["email"] != "alice@example.com" {
		t.Fatalf("unexpected first email: %v", addrs[0])
	}
}

func TestDisplayFolderName(t *testing.T) {
	if got := displayFolderName("Label_42", "custom"); got != "Label_42" {
		t.Fatalf("custom label: %q", got)
	}
	if got := displayFolderName("INBOX", "inbox"); got != "Boîte de réception" {
		t.Fatalf("inbox display: %q", got)
	}
}
