package rest_test

import (
	"bytes"
	"io"
	"mime/multipart"
	"net/http"
	"net/http/httptest"
	"os"
	"strings"
	"testing"

	"github.com/pocketbase/pocketbase/tools/rest"
)

func TestFindUploadedFiles(t *testing.T) {
	// create a test temporary file
	tmpFile, err := os.CreateTemp(os.TempDir(), "tmpfile-*.txt")
	if err != nil {
		t.Fatal(err)
	}
	if _, err := tmpFile.Write([]byte("test")); err != nil {
		t.Fatal(err)
	}
	tmpFile.Seek(0, 0)
	defer tmpFile.Close()
	defer os.Remove(tmpFile.Name())
	// ---

	// stub multipart form file body
	body := new(bytes.Buffer)
	mp := multipart.NewWriter(body)
	w, err := mp.CreateFormFile("test", tmpFile.Name())
	if err != nil {
		t.Fatal(err)
	}
	if _, err := io.Copy(w, tmpFile); err != nil {
		t.Fatal(err)
	}
	mp.Close()
	// ---

	req := httptest.NewRequest(http.MethodPost, "/", body)
	req.Header.Add("Content-Type", mp.FormDataContentType())

	result, err := rest.FindUploadedFiles(req, "test")
	if err != nil {
		t.Fatal(err)
	}

	if len(result) != 1 {
		t.Fatalf("Expected 1 file, got %d", len(result))
	}

	if result[0].Header().Size != 4 {
		t.Fatalf("Expected the file size to be 4 bytes, got %d", result[0].Header().Size)
	}

	if !strings.HasSuffix(result[0].Name(), ".txt") {
		t.Fatalf("Expected the file name to have suffix .txt - %v", result[0].Name())
	}

	if string(result[0].Bytes()) != "test" {
		t.Fatalf("Expected the file content to be %q, got %q", "test", string(result[0].Bytes()))
	}
}

func TestFindUploadedFilesMissing(t *testing.T) {
	body := new(bytes.Buffer)
	mp := multipart.NewWriter(body)
	mp.Close()

	req := httptest.NewRequest(http.MethodPost, "/", body)
	req.Header.Add("Content-Type", mp.FormDataContentType())

	result, err := rest.FindUploadedFiles(req, "test")
	if err == nil {
		t.Error("Expected error, got nil")
	}

	if result != nil {
		t.Errorf("Expected result to be nil, got %v", result)
	}
}
