package handlers

import "testing"

func TestIsAllowedUpload(t *testing.T) {
	cases := []struct {
		name string
		mime string
		ext  string
		want bool
	}{
		{"image", "image/png", ".png", true},
		{"pdf", "application/pdf", ".pdf", true},
		{"md", "text/markdown", ".md", true},
		{"md_plain", "text/plain", ".md", true},
		{"other", "application/zip", ".zip", false},
	}

	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			if got := isAllowedUpload(c.mime, c.ext); got != c.want {
				t.Fatalf("attendu %v, obtenu %v", c.want, got)
			}
		})
	}
}

func TestMaxUploadBytes(t *testing.T) {
	t.Setenv("MAX_UPLOAD_MB", "1")
	if got := maxUploadBytes(); got != 1*1024*1024 {
		t.Fatalf("taille inattendue: %d", got)
	}

	t.Setenv("MAX_UPLOAD_MB", "0")
	if got := maxUploadBytes(); got != 50*1024*1024 {
		t.Fatalf("taille par defaut inattendue: %d", got)
	}
}

func TestParseTypeChamp(t *testing.T) {
	valid := []string{"string", "int", "bool", "date"}
	for _, v := range valid {
		if _, ok := parseTypeChamp(v); !ok {
			t.Fatalf("type_champ invalide: %s", v)
		}
	}
	if _, ok := parseTypeChamp("x"); ok {
		t.Fatal("type_champ devrait etre invalide")
	}
}

func TestParseLienType(t *testing.T) {
	valid := []string{"stocke", "utilise_dans"}
	for _, v := range valid {
		if _, ok := parseLienType(v); !ok {
			t.Fatalf("type lien invalide: %s", v)
		}
	}
	if _, ok := parseLienType("x"); ok {
		t.Fatal("type lien devrait etre invalide")
	}
}

func TestParseStatut(t *testing.T) {
	valid := []string{"en_stock", "pret", "hors_service", "archive"}
	for _, v := range valid {
		if _, ok := parseStatut(v); !ok {
			t.Fatalf("statut invalide: %s", v)
		}
	}
	if _, ok := parseStatut("x"); ok {
		t.Fatal("statut devrait etre invalide")
	}
}
