package search

import (
	"testing"

	"github.com/stretchr/testify/assert"

	"github.com/photoprism/photoprism/internal/form"
)

func TestPhotosFilterAlt(t *testing.T) {
	t.Run("Ten", func(t *testing.T) {
		var f form.SearchPhotos

		f.Alt = "-10"
		f.Merged = true

		photos, _, err := Photos(f)

		if err != nil {
			t.Fatal(err)
		}

		for _, r := range photos {
			assert.GreaterOrEqual(t, -10, r.PhotoAltitude)
			assert.LessOrEqual(t, -10, r.PhotoAltitude)
		}
		assert.Len(t, photos, 1)
	})
	t.Run("Num100Five", func(t *testing.T) {
		var f form.SearchPhotos

		f.Alt = "-100--5"
		f.Merged = true

		photos, _, err := Photos(f)

		if err != nil {
			t.Fatal(err)
		}

		for _, r := range photos {
			assert.GreaterOrEqual(t, -5, r.PhotoAltitude)
			assert.LessOrEqual(t, -100, r.PhotoAltitude)
		}

		assert.Len(t, photos, 2)
	})
	t.Run("Num200Num500", func(t *testing.T) {
		var f form.SearchPhotos

		f.Alt = "200-500"
		f.Merged = true

		photos, _, err := Photos(f)

		if err != nil {
			t.Fatal(err)
		}

		for _, r := range photos {
			assert.GreaterOrEqual(t, 500, r.PhotoAltitude)
			assert.LessOrEqual(t, 200, r.PhotoAltitude)
		}

		assert.Len(t, photos, 2)
	})
	t.Run("Num200", func(t *testing.T) {
		var f form.SearchPhotos

		f.Alt = "200"
		f.Merged = true

		photos, _, err := Photos(f)

		if err != nil {
			t.Fatal(err)
		}

		assert.Len(t, photos, 1)
	})
	t.Run("Invalid", func(t *testing.T) {
		var f form.SearchPhotos

		f.Alt = "%gold"
		f.Merged = true

		photos, _, err := Photos(f)

		if err != nil {
			t.Fatal(err)
		}
		assert.GreaterOrEqual(t, len(photos), 40)
	})
}

func TestPhotosQueryAlt(t *testing.T) {
	t.Run("Ten", func(t *testing.T) {
		var f form.SearchPhotos

		f.Query = "alt:\"-10\""
		f.Merged = true

		photos, _, err := Photos(f)

		if err != nil {
			t.Fatal(err)
		}

		for _, r := range photos {
			assert.GreaterOrEqual(t, -10, r.PhotoAltitude)
			assert.LessOrEqual(t, -10, r.PhotoAltitude)
		}

		assert.Len(t, photos, 1)
	})
	t.Run("Num100Five", func(t *testing.T) {
		var f form.SearchPhotos

		f.Query = "alt:\"-100--5\""
		f.Merged = true

		photos, _, err := Photos(f)

		if err != nil {
			t.Fatal(err)
		}

		for _, r := range photos {
			assert.GreaterOrEqual(t, -5, r.PhotoAltitude)
			assert.LessOrEqual(t, -100, r.PhotoAltitude)
		}

		assert.Len(t, photos, 2)
	})
	t.Run("Num200Num500", func(t *testing.T) {
		var f form.SearchPhotos

		f.Query = "alt:\"200-500\""
		f.Merged = true

		photos, _, err := Photos(f)

		if err != nil {
			t.Fatal(err)
		}

		for _, r := range photos {
			assert.GreaterOrEqual(t, 500, r.PhotoAltitude)
			assert.LessOrEqual(t, 200, r.PhotoAltitude)
		}

		assert.Len(t, photos, 2)
	})
	t.Run("Num200", func(t *testing.T) {
		var f form.SearchPhotos

		f.Query = "alt:\"200\""
		f.Merged = true

		photos, _, err := Photos(f)

		if err != nil {
			t.Fatal(err)
		}
		assert.Len(t, photos, 1)
	})
	t.Run("Invalid", func(t *testing.T) {
		var f form.SearchPhotos

		f.Query = "alt:\"%gold\""
		f.Merged = true

		photos, _, err := Photos(f)

		if err != nil {
			t.Fatal(err)
		}
		assert.GreaterOrEqual(t, len(photos), 40)
	})
}
