package utils

import (
	"testing"
)

func TestFormatPhoneToJID(t *testing.T) {
	tests := []struct {
		name               string
		phone              string
		defaultCountryCode string
		want               string
	}{
		{
			name:               "Already in JID format",
			phone:              "27834606792@s.whatsapp.net",
			defaultCountryCode: "27",
			want:               "27834606792@s.whatsapp.net",
		},
		{
			name:               "Plain number with leading zero",
			phone:              "0834606792",
			defaultCountryCode: "27",
			want:               "27834606792@s.whatsapp.net",
		},
		{
			name:               "Number with country code",
			phone:              "27834606792",
			defaultCountryCode: "27",
			want:               "27834606792@s.whatsapp.net",
		},
		{
			name:               "Number with plus sign",
			phone:              "+27834606792",
			defaultCountryCode: "27",
			want:               "27834606792@s.whatsapp.net",
		},
		{
			name:               "Number without country code config",
			phone:              "0834606792",
			defaultCountryCode: "",
			want:               "834606792@s.whatsapp.net",
		},
		{
			name:               "Number with spaces and dashes",
			phone:              "083-460-6792",
			defaultCountryCode: "27",
			want:               "27834606792@s.whatsapp.net",
		},
		{
			name:               "Number with parentheses",
			phone:              "(083) 460 6792",
			defaultCountryCode: "27",
			want:               "27834606792@s.whatsapp.net",
		},
		{
			name:               "US number with leading 1",
			phone:              "12025551234",
			defaultCountryCode: "1",
			want:               "12025551234@s.whatsapp.net",
		},
		{
			name:               "US number with area code",
			phone:              "202-555-1234",
			defaultCountryCode: "1",
			want:               "12025551234@s.whatsapp.net",
		},
		{
			name:               "Group JID unchanged",
			phone:              "123456789-1234567890@g.us",
			defaultCountryCode: "27",
			want:               "123456789-1234567890@g.us",
		},
		{
			name:               "Number with different country code via plus sign",
			phone:              "+12025551234",
			defaultCountryCode: "27",
			want:               "12025551234@s.whatsapp.net",
		},
		{
			name:               "Country code with plus in config",
			phone:              "0834606792",
			defaultCountryCode: "+27",
			want:               "27834606792@s.whatsapp.net",
		},
		{
			name:               "Empty phone number",
			phone:              "",
			defaultCountryCode: "27",
			want:               "",
		},
		{
			name:               "Multiple leading zeros",
			phone:              "00834606792",
			defaultCountryCode: "27",
			want:               "27834606792@s.whatsapp.net",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := FormatPhoneToJID(tt.phone, tt.defaultCountryCode)
			if got != tt.want {
				t.Errorf("FormatPhoneToJID(%q, %q) = %q, want %q",
					tt.phone, tt.defaultCountryCode, got, tt.want)
			}
		})
	}
}

func TestIsGroupJID(t *testing.T) {
	tests := []struct {
		name string
		jid  string
		want bool
	}{
		{
			name: "Individual JID",
			jid:  "27834606792@s.whatsapp.net",
			want: false,
		},
		{
			name: "Group JID",
			jid:  "123456789-1234567890@g.us",
			want: true,
		},
		{
			name: "Empty string",
			jid:  "",
			want: false,
		},
		{
			name: "Invalid JID",
			jid:  "notajid",
			want: false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := IsGroupJID(tt.jid)
			if got != tt.want {
				t.Errorf("IsGroupJID(%q) = %v, want %v", tt.jid, got, tt.want)
			}
		})
	}
}

func TestIsValidJID(t *testing.T) {
	tests := []struct {
		name string
		jid  string
		want bool
	}{
		{
			name: "Valid individual JID",
			jid:  "27834606792@s.whatsapp.net",
			want: true,
		},
		{
			name: "Valid group JID",
			jid:  "123456789-1234567890@g.us",
			want: true,
		},
		{
			name: "Valid broadcast JID",
			jid:  "123456789@broadcast",
			want: true,
		},
		{
			name: "Invalid - no @ symbol",
			jid:  "27834606792",
			want: false,
		},
		{
			name: "Invalid - wrong suffix",
			jid:  "27834606792@invalid.com",
			want: false,
		},
		{
			name: "Empty string",
			jid:  "",
			want: false,
		},
		{
			name: "Just @ symbol",
			jid:  "@",
			want: false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := IsValidJID(tt.jid)
			if got != tt.want {
				t.Errorf("IsValidJID(%q) = %v, want %v", tt.jid, got, tt.want)
			}
		})
	}
}
