package repository

import (
	"context"
	"os"
	"testing"
	"time"

	"apps/backend/internal/module/scout/domain"
)

func TestMongoRunIntegration(t *testing.T) {
	uri := os.Getenv("SCOUT_MONGO_URI")
	database := os.Getenv("SCOUT_MONGO_DATABASE")
	if uri == "" || database == "" {
		t.Skip("SCOUT_MONGO_URI and SCOUT_MONGO_DATABASE are required for MongoRun integration")
	}
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	store := NewMonStore(uri, database)
	run := memoryRun("mongo-run-test", 900001, domain.NowNano(), domain.RunSucceeded)
	if err := store.CreateRun(ctx, run); err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { _ = store.DeleteRun(context.Background(), run.OwnerUID, run.ID) })
	page, err := store.ListRuns(ctx, domain.RunFilter{OwnerUID: run.OwnerUID, Page: 1, PageSize: 10})
	if err != nil || page.Pagination.Total < 1 {
		t.Fatalf("run not listed: %+v %v", page, err)
	}
	if err := store.ReplaceRunGuarded(ctx, run.OwnerUID, run.ID, []string{domain.RunSucceeded}, run); err != nil {
		t.Fatalf("guarded replacement failed: %v", err)
	}
}
