package feedback

import "testing"

func TestAllowedAttachment(t *testing.T) {
	tests := []struct {
		name string
		file string
		want bool
	}{
		{"png image", "shot.png", true},
		{"jpeg upper-case ext", "Photo.JPG", true},
		{"pdf doc", "report.pdf", true},
		{"archive 7z", "logs.7z", true},
		{"doc with dotted name", "my.notes.docx", true},
		{"disallowed exe", "evil.exe", false},
		{"disallowed svg (xss vector)", "x.svg", false},
		{"disallowed html", "x.html", false},
		{"no extension", "README", false},
		{"empty name", "", false},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := AllowedAttachment(tt.file); got != tt.want {
				t.Errorf("AllowedAttachment(%q) = %v, want %v", tt.file, got, tt.want)
			}
		})
	}
}

func TestIsImageAndContentType(t *testing.T) {
	tests := []struct {
		file    string
		isImage bool
		ctype   string
	}{
		{"a.png", true, "image/png"},
		{"a.jpg", true, "image/jpeg"},
		{"a.jpeg", true, "image/jpeg"},
		{"a.webp", true, "image/webp"},
		{"a.gif", true, "image/gif"},
		{"a.pdf", false, "application/octet-stream"},
		{"a.zip", false, "application/octet-stream"},
		{"a.svg", false, "application/octet-stream"}, // even if it slipped past, never image/svg+xml
		{"noext", false, "application/octet-stream"},
	}
	for _, tt := range tests {
		t.Run(tt.file, func(t *testing.T) {
			if got := IsImage(tt.file); got != tt.isImage {
				t.Errorf("IsImage(%q) = %v, want %v", tt.file, got, tt.isImage)
			}
			if got := ContentType(tt.file); got != tt.ctype {
				t.Errorf("ContentType(%q) = %q, want %q", tt.file, got, tt.ctype)
			}
		})
	}
}

func TestNormalizeChannel(t *testing.T) {
	tests := []struct {
		in   string
		want string
	}{
		{"telegram", "telegram"},
		{"ios", "ios"},
		{"android", "android"},
		{"web", "web"},
		{"  iOS ", "ios"}, // trimmed + lower-cased
		{"TELEGRAM", "telegram"},
		{"", "web"},        // unknown -> web
		{"windows", "web"}, // unknown -> web
		{"'; DROP", "web"}, // junk -> web
	}
	for _, tt := range tests {
		t.Run(tt.in, func(t *testing.T) {
			if got := normalizeChannel(tt.in); got != tt.want {
				t.Errorf("normalizeChannel(%q) = %q, want %q", tt.in, got, tt.want)
			}
		})
	}
}
