package photos

import (
	"testing"

	"github.com/ultisuite/ulti-backend/internal/api/apivalidate"
)

func hasFieldDetail(err *apivalidate.ValidationError, field, message string) bool {
	if err == nil {
		return false
	}
	for _, d := range err.Details {
		if d.Field == field && d.Message == message {
			return true
		}
	}
	return false
}

func TestValidateAlbumID(t *testing.T) {
	if validateAlbumID("album-1") != nil {
		t.Fatal("expected valid album id")
	}
	if err := validateAlbumID(""); !hasFieldDetail(err, "album_id", "required") {
		t.Fatal("expected required album_id error")
	}
}

func TestValidateCreateAlbum(t *testing.T) {
	if validateCreateAlbum(&createAlbumRequest{Name: "Vacation"}) != nil {
		t.Fatal("expected valid create album request")
	}
	if err := validateCreateAlbum(&createAlbumRequest{}); !hasFieldDetail(err, "name", "required") {
		t.Fatal("expected missing name error")
	}
	if err := validateCreateAlbum(&createAlbumRequest{Name: "Trip", AssetIDs: []string{""}}); !hasFieldDetail(err, "asset_ids", "invalid") {
		t.Fatal("expected invalid asset_ids error")
	}
	if err := validateCreateAlbum(&createAlbumRequest{Name: "Bad\nName"}); !hasFieldDetail(err, "name", "invalid") {
		t.Fatal("expected invalid name error")
	}
}

func TestValidateUpdateAlbum(t *testing.T) {
	name := "Renamed"
	if validateUpdateAlbum(&updateAlbumRequest{Name: &name}) != nil {
		t.Fatal("expected valid update album request")
	}
	desc := "Notes"
	if validateUpdateAlbum(&updateAlbumRequest{Description: &desc}) != nil {
		t.Fatal("expected valid description-only update")
	}
	if err := validateUpdateAlbum(&updateAlbumRequest{}); !hasFieldDetail(err, "body", "required") {
		t.Fatal("expected empty body error")
	}
	blank := "  "
	if err := validateUpdateAlbum(&updateAlbumRequest{Name: &blank}); !hasFieldDetail(err, "name", "required") {
		t.Fatal("expected blank name error")
	}
}

func TestValidateAlbumAssetsRequest(t *testing.T) {
	if validateAlbumAssetsRequest(&albumAssetsRequest{AssetIDs: []string{"a1"}}) != nil {
		t.Fatal("expected valid album assets request")
	}
	if err := validateAlbumAssetsRequest(&albumAssetsRequest{}); !hasFieldDetail(err, "asset_ids", "required") {
		t.Fatal("expected required asset_ids error")
	}
	if err := validateAlbumAssetsRequest(&albumAssetsRequest{AssetIDs: []string{" "}}); !hasFieldDetail(err, "asset_ids", "invalid") {
		t.Fatal("expected invalid asset_ids error")
	}
}
