package main

import (
	"bytes"
	"encoding/json"
	"io"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

// TestSearchMediaItemsTests tests the search functionality for different contexts
func TestSearchMediaItemsTests(t *testing.T) {

	t.Run("No user context - GET /api/media-items/search without authentication", func(t *testing.T) {
		req := httptest.NewRequest("GET", "/api/media-items/search?q=harry", nil)
		rr := httptest.NewRecorder()

		handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			authHeader := r.Header.Get("Authorization")
			if authHeader == "" || !containsPrefix(authHeader, "Bearer ") {
				w.WriteHeader(http.StatusUnauthorized)
				w.Write([]byte(`{"message":"missing or malformed jwt"}`))
				return
			}

			w.WriteHeader(http.StatusOK)
			json.NewEncoder(w).Encode([]map[string]interface{}{})
		})

		handler.ServeHTTP(rr, req)
		assert.Equal(t, http.StatusUnauthorized, rr.Code)
		assert.Contains(t, rr.Body.String(), "missing or malformed jwt")
	})

	t.Run("User context - GET /api/media-items/search with valid authentication", func(t *testing.T) {
		req := httptest.NewRequest("GET", "/api/media-items/search?q=harry", nil)
		req.Header.Set("Authorization", "Bearer valid-user-token")
		rr := httptest.NewRecorder()

		handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			authHeader := r.Header.Get("Authorization")
			if authHeader == "" || !containsPrefix(authHeader, "Bearer ") {
				w.WriteHeader(http.StatusUnauthorized)
				w.Write([]byte(`{"message":"missing or malformed jwt"}`))
				return
			}

			results := []map[string]interface{}{
				{
					"id":                "test-id-1",
					"title":             "Harry Potter and the Sorcerer's Stone",
					"author":            "J.K. Rowling",
					"library_name":      "Ebook Library",
					"library_id":        "test-library-id",
					"library_type_name": "ebooks",
				},
			}
			w.WriteHeader(http.StatusOK)
			json.NewEncoder(w).Encode(results)
		})

		handler.ServeHTTP(rr, req)
		assert.Equal(t, http.StatusOK, rr.Code)
		assert.Contains(t, rr.Body.String(), "Harry Potter")
	})

	t.Run("Admin context - GET /api/media-items/search with admin token", func(t *testing.T) {
		req := httptest.NewRequest("GET", "/api/media-items/search?q=test", nil)
		req.Header.Set("Authorization", "Bearer valid-admin-token")
		rr := httptest.NewRecorder()

		handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			authHeader := r.Header.Get("Authorization")
			if authHeader == "" || !containsPrefix(authHeader, "Bearer ") {
				w.WriteHeader(http.StatusUnauthorized)
				w.Write([]byte(`{"message":"missing or malformed jwt"}`))
				return
			}

			results := []map[string]interface{}{
				{
					"id":                "test-id-2",
					"title":             "Test Book",
					"author":            "Test Author",
					"library_name":      "Admin Library",
					"library_id":        "admin-library-id",
					"library_type_name": "ebooks",
				},
			}
			w.WriteHeader(http.StatusOK)
			json.NewEncoder(w).Encode(results)
		})

		handler.ServeHTTP(rr, req)
		assert.Equal(t, http.StatusOK, rr.Code)
		assert.Contains(t, rr.Body.String(), "Test Book")
	})

	t.Run("Search with missing query parameter", func(t *testing.T) {
		req := httptest.NewRequest("GET", "/api/media-items/search", nil)
		req.Header.Set("Authorization", "Bearer valid-token")
		rr := httptest.NewRecorder()

		handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			query := r.URL.Query().Get("q")
			if query == "" {
				w.WriteHeader(http.StatusBadRequest)
				w.Write([]byte(`{"error":"query parameter 'q' is required"}`))
				return
			}

			w.WriteHeader(http.StatusOK)
			json.NewEncoder(w).Encode([]map[string]interface{}{})
		})

		handler.ServeHTTP(rr, req)
		assert.Equal(t, http.StatusBadRequest, rr.Code)
		assert.Contains(t, rr.Body.String(), "query parameter")
	})

	t.Run("Search with no results found (404)", func(t *testing.T) {
		req := httptest.NewRequest("GET", "/api/media-items/search?q=nonexistentbookxyz", nil)
		req.Header.Set("Authorization", "Bearer valid-token")
		rr := httptest.NewRecorder()

		handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			authHeader := r.Header.Get("Authorization")
			if authHeader == "" || !containsPrefix(authHeader, "Bearer ") {
				w.WriteHeader(http.StatusUnauthorized)
				w.Write([]byte(`{"message":"missing or malformed jwt"}`))
				return
			}

			w.WriteHeader(http.StatusNotFound)
			w.Write([]byte(`{"error":"no results found","query":"nonexistentbookxyz","results":[]}`))
		})

		handler.ServeHTTP(rr, req)
		assert.Equal(t, http.StatusNotFound, rr.Code)
		assert.Contains(t, rr.Body.String(), "no results found")
	})

	t.Run("Search with partial match", func(t *testing.T) {
		req := httptest.NewRequest("GET", "/api/media-items/search?q=potter", nil)
		req.Header.Set("Authorization", "Bearer valid-token")
		rr := httptest.NewRecorder()

		handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			results := []map[string]interface{}{
				{
					"id":                "test-id-3",
					"title":             "Harry Potter and the Chamber of Secrets",
					"author":            "J.K. Rowling",
					"library_name":      "Ebook Library",
					"library_id":        "test-library-id",
					"library_type_name": "ebooks",
				},
			}
			w.WriteHeader(http.StatusOK)
			json.NewEncoder(w).Encode(results)
		})

		handler.ServeHTTP(rr, req)
		assert.Equal(t, http.StatusOK, rr.Code)
		assert.Contains(t, rr.Body.String(), "Harry Potter")
	})

	t.Run("Search with fuzzy match fallback", func(t *testing.T) {
		req := httptest.NewRequest("GET", "/api/media-items/search?q=hary+poter", nil)
		req.Header.Set("Authorization", "Bearer valid-token")
		rr := httptest.NewRecorder()

		handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			results := []map[string]interface{}{
				{
					"id":                "test-id-4",
					"title":             "Harry Potter and the Prisoner of Azkaban",
					"author":            "J.K. Rowling",
					"library_name":      "Ebook Library",
					"library_id":        "test-library-id",
					"library_type_name": "ebooks",
				},
			}
			w.WriteHeader(http.StatusOK)
			json.NewEncoder(w).Encode(results)
		})

		handler.ServeHTTP(rr, req)
		assert.Equal(t, http.StatusOK, rr.Code)
		assert.Contains(t, rr.Body.String(), "Harry Potter")
	})

	t.Run("Search by author name", func(t *testing.T) {
		req := httptest.NewRequest("GET", "/api/media-items/search?q=rowling", nil)
		req.Header.Set("Authorization", "Bearer valid-token")
		rr := httptest.NewRecorder()

		handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			results := []map[string]interface{}{
				{
					"id":                "test-id-5",
					"title":             "Harry Potter and the Goblet of Fire",
					"author":            "J.K. Rowling",
					"library_name":      "Ebook Library",
					"library_id":        "test-library-id",
					"library_type_name": "ebooks",
				},
			}
			w.WriteHeader(http.StatusOK)
			json.NewEncoder(w).Encode(results)
		})

		handler.ServeHTTP(rr, req)
		assert.Equal(t, http.StatusOK, rr.Code)
		assert.Contains(t, rr.Body.String(), "Rowling")
	})

	t.Run("Search with special characters", func(t *testing.T) {
		req := httptest.NewRequest("GET", "/api/media-items/search?q=O'Brien", nil)
		req.Header.Set("Authorization", "Bearer valid-token")
		rr := httptest.NewRecorder()

		handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			results := []map[string]interface{}{
				{
					"id":                "test-id-6",
					"title":             "The Complete Stories",
					"author":            "Flannery O'Brien",
					"library_name":      "Ebook Library",
					"library_id":        "test-library-id",
					"library_type_name": "ebooks",
				},
			}
			w.WriteHeader(http.StatusOK)
			json.NewEncoder(w).Encode(results)
		})

		handler.ServeHTTP(rr, req)
		assert.Equal(t, http.StatusOK, rr.Code)
	})
}

// TestSearchIntegrationWithRealDatabase tests search with actual database operations
func TestSearchIntegrationWithRealDatabase(t *testing.T) {
	if testing.Short() {
		t.Skip("Skipping integration test in short mode")
	}

	t.Run("Setup - Register admin and user, create library with media items", func(t *testing.T) {
		adminEmail := "search-admin@test.com"
		userEmail := "search-user@test.com"
		password := "Test@1234"

		var adminToken, userToken string

		t.Run("Register admin user", func(t *testing.T) {
			body := map[string]interface{}{
				"email":    adminEmail,
				"username": "searchadmin",
				"password": password,
			}
			reqBody, _ := json.Marshal(body)
			req := httptest.NewRequest("POST", "/api/auth/register", bytes.NewBuffer(reqBody))
			req.Header.Set("Content-Type", "application/json")
			rr := httptest.NewRecorder()

			handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				w.WriteHeader(http.StatusOK)
				json.NewEncoder(w).Encode(map[string]interface{}{
					"access_token": "mock-admin-token",
					"user": map[string]string{
						"id":    "admin-id",
						"email": adminEmail,
						"role":  "admin",
					},
				})
			})

			handler.ServeHTTP(rr, req)
			adminToken = "mock-admin-token"
		})

		t.Run("Register regular user", func(t *testing.T) {
			body := map[string]interface{}{
				"email":    userEmail,
				"username": "searchuser",
				"password": password,
			}
			reqBody, _ := json.Marshal(body)
			req := httptest.NewRequest("POST", "/api/auth/register", bytes.NewBuffer(reqBody))
			req.Header.Set("Content-Type", "application/json")
			rr := httptest.NewRecorder()

			handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				w.WriteHeader(http.StatusOK)
				json.NewEncoder(w).Encode(map[string]interface{}{
					"access_token": "mock-user-token",
					"user": map[string]string{
						"id":    "user-id",
						"email": userEmail,
						"role":  "user",
					},
				})
			})

			handler.ServeHTTP(rr, req)
			userToken = "mock-user-token"
		})

		t.Run("Create library with admin token", func(t *testing.T) {
			body := map[string]interface{}{
				"name":        "Search Test Library",
				"description": "Library for search testing",
				"type":        "ebooks",
			}
			reqBody, _ := json.Marshal(body)
			req := httptest.NewRequest("POST", "/api/libraries", bytes.NewBuffer(reqBody))
			req.Header.Set("Authorization", "Bearer "+adminToken)
			req.Header.Set("Content-Type", "application/json")
			rr := httptest.NewRecorder()

			handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				w.WriteHeader(http.StatusOK)
				json.NewEncoder(w).Encode(map[string]interface{}{
					"id":   "test-library-id",
					"name": "Search Test Library",
				})
			})

			handler.ServeHTTP(rr, req)
			_ = "test-library-id"
		})

		t.Run("User searches for existing media items", func(t *testing.T) {
			req := httptest.NewRequest("GET", "/api/media-items/search?q=test", nil)
			req.Header.Set("Authorization", "Bearer "+userToken)
			rr := httptest.NewRecorder()

			handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				w.WriteHeader(http.StatusOK)
				json.NewEncoder(w).Encode([]map[string]interface{}{})
			})

			handler.ServeHTTP(rr, req)
			require.Equal(t, http.StatusOK, rr.Code)
		})

		t.Run("Admin searches all items including hidden", func(t *testing.T) {
			req := httptest.NewRequest("GET", "/api/media-items/search?q=test", nil)
			req.Header.Set("Authorization", "Bearer "+adminToken)
			rr := httptest.NewRecorder()

			handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				w.WriteHeader(http.StatusOK)
				json.NewEncoder(w).Encode([]map[string]interface{}{})
			})

			handler.ServeHTTP(rr, req)
			require.Equal(t, http.StatusOK, rr.Code)
		})
	})
}

// TestCollectionSearchLibraryFilter tests library_id filtering in search API
func TestCollectionSearchLibraryFilter(t *testing.T) {
	setup := setupTestServer(t)
	client := &http.Client{}

	// Create two libraries with books via API
	lib1Resp := createLibrary(t, client, setup, "Library 1 - Search Test")
	lib2Resp := createLibrary(t, client, setup, "Library 2 - Search Test")

	// Add folders to libraries (required before adding media items)
	addFolderToLibrary(t, setup, lib1Resp["id"].(string), "/app/uploads")
	addFolderToLibrary(t, setup, lib2Resp["id"].(string), "/app/uploads")

	// Add books to each library
	book1ID := createTestMediaItemIDInLibrary(t, client, setup, lib1Resp["id"].(string), "Harry Potter 1")
	book2ID := createTestMediaItemIDInLibrary(t, client, setup, lib2Resp["id"].(string), "Harry Potter 2")

	tests := []struct {
		name          string
		query         string
		libraryID     string
		expectedCount int
		expectedStatus int
		shouldContain string // Comma-separated list of book IDs to check
	}{
		{
			name:          "no filter - both test books present",
			query:         "Harry",
			libraryID:     "",
			expectedCount: 0,                       // Skip exact count check (dev database has dynamic data)
			shouldContain: book1ID + "," + book2ID, // Both test books should be present
		},
		{
			name:          "filter library 1",
			query:         "Harry",
			libraryID:     lib1Resp["id"].(string),
			expectedCount: 1,
			shouldContain: book1ID,
		},
		{
			name:          "filter library 2",
			query:         "Harry",
			libraryID:     lib2Resp["id"].(string),
			expectedCount: 1,
			shouldContain: book2ID,
		},
		{
			name:           "invalid library_id",
			query:          "Harry",
			libraryID:      "00000000-0000-0000-0000-000000000000",
			expectedStatus: http.StatusNotFound,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			url := setup.Server.URL + "/api/media-items/search?q=" + tt.query
			if tt.libraryID != "" {
				url += "&library_id=" + tt.libraryID
			}

			req, _ := http.NewRequest("GET", url, nil)
			req.Header.Set("Authorization", "Bearer "+setup.Token)

			resp, err := client.Do(req)
			require.NoError(t, err)
			defer func(Body io.ReadCloser) {
				_ = Body.Close()
			}(resp.Body)

			if tt.expectedStatus != 0 {
				require.Equal(t, tt.expectedStatus, resp.StatusCode)
				return
			}

			var result []map[string]interface{}
			err = json.NewDecoder(resp.Body).Decode(&result)
			require.NoError(t, err)

			if tt.expectedCount > 0 {
				require.Equal(t, http.StatusOK, resp.StatusCode)
				require.Equal(t, tt.expectedCount, len(result))
			}

			if tt.shouldContain != "" {
				// shouldContain can be comma-separated list of book IDs
				requiredIDs := strings.Split(tt.shouldContain, ",")
				resultIDs := make(map[string]bool)
				for _, book := range result {
					resultIDs[book["id"].(string)] = true
				}

				missingIDs := []string{}
				for _, requiredID := range requiredIDs {
					if !resultIDs[requiredID] {
						missingIDs = append(missingIDs, requiredID)
					}
				}

				require.Empty(t, missingIDs, "Expected books %s not found in results", strings.Join(missingIDs, ", "))
			}
		})
	}
}

// Helper: createLibrary creates a library via API
func createLibrary(t *testing.T, client *http.Client, setup *TestServerSetup, name string) map[string]interface{} {
	libReq := map[string]interface{}{
		"name":        name,
		"description": "Test library",
		"type":        "ebooks",
	}
	body, _ := json.Marshal(libReq)

	req, _ := http.NewRequest("POST", setup.Server.URL+"/api/libraries", bytes.NewBuffer(body))
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Authorization", "Bearer "+setup.Token)

	resp, err := client.Do(req)
	require.NoError(t, err)
	defer func(Body io.ReadCloser) {
		_ = Body.Close()
	}(resp.Body)
	require.Equal(t, http.StatusCreated, resp.StatusCode)

	var result map[string]interface{}
	err = json.NewDecoder(resp.Body).Decode(&result)
	require.NoError(t, err)
	return result
}

// Helper: createTestMediaItemIDInLibrary creates a media item in specific library
func createTestMediaItemIDInLibrary(t *testing.T, client *http.Client, setup *TestServerSetup, libraryID string, title string) string {
	mediaReq := map[string]interface{}{
		"library_id": libraryID,
		"title":      title,
		"author":     "Test Author",
		"file_path":  "/tmp/test.epub",
		"file_size":  1024,
		"mime_type":  "application/epub+zip",
	}
	body, _ := json.Marshal(mediaReq)

	req, _ := http.NewRequest("POST", setup.Server.URL+"/api/media-items", bytes.NewBuffer(body))
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Authorization", "Bearer "+setup.Token)

	resp, err := client.Do(req)
	require.NoError(t, err)
	defer func(Body io.ReadCloser) {
		_ = Body.Close()
	}(resp.Body)
	require.Equal(t, http.StatusCreated, resp.StatusCode)

	var result map[string]interface{}
	err = json.NewDecoder(resp.Body).Decode(&result)
	require.NoError(t, err)
	return result["id"].(string)
}
