package server

import (
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"testing"
)

func TestHandleGetFileTypes(t *testing.T) {
	s := &Server{}
	req := httptest.NewRequest(http.MethodGet, "/api/file-types", nil)
	rec := httptest.NewRecorder()

	s.handleGetFileTypes(rec, req)
	if rec.Code != http.StatusOK {
		t.Fatalf("expected 200, got %d", rec.Code)
	}

	var payload struct {
		ArchiveExtensions []string `json:"archive_extensions"`
		UploadExtensions  []string `json:"upload_extensions"`
		ConvertExtensions []string `json:"convert_extensions"`
	}
	if err := json.NewDecoder(rec.Body).Decode(&payload); err != nil {
		t.Fatalf("decode payload: %v", err)
	}
	if len(payload.ArchiveExtensions) == 0 || len(payload.UploadExtensions) == 0 || len(payload.ConvertExtensions) == 0 {
		t.Fatalf("expected non-empty extensions in payload")
	}
	if !containsString(payload.ArchiveExtensions, ".gz") {
		t.Fatalf("expected .gz in archive extensions")
	}
	if !containsString(payload.UploadExtensions, ".json") || !containsString(payload.ConvertExtensions, ".json") {
		t.Fatalf("expected .json in upload/convert extensions")
	}
}

func containsString(items []string, target string) bool {
	for _, item := range items {
		if item == target {
			return true
		}
	}
	return false
}
