package sqlite

import (
	"context"
	"testing"
)

func TestImportRunItemEventsRepoAppendValidationAndDefaults(t *testing.T) {
	t.Parallel()

	ctx := context.Background()
	store := openTestDB(t)
	repo := store.ImportRunEvents()

	if err := repo.Append(ctx, ImportRunItemEvent{}); err == nil || err.Error() != "event_id is required" {
		t.Fatalf("Append() error = %v, want event_id is required", err)
	}
	if err := repo.Append(ctx, ImportRunItemEvent{EventID: "evt-1"}); err == nil || err.Error() != "run_id is required" {
		t.Fatalf("Append() error = %v, want run_id is required", err)
	}
	if err := repo.Append(ctx, ImportRunItemEvent{EventID: "evt-1", RunID: "run-1"}); err == nil || err.Error() != "item_id is required" {
		t.Fatalf("Append() error = %v, want item_id is required", err)
	}
	if err := repo.Append(ctx, ImportRunItemEvent{EventID: "evt-1", RunID: "run-1", ItemID: "item-1"}); err == nil || err.Error() != "event_type is required" {
		t.Fatalf("Append() error = %v, want event_type is required", err)
	}
	if err := repo.Append(ctx, ImportRunItemEvent{EventID: "evt-1", RunID: "run-1", ItemID: "item-1", EventType: "confirm"}); err == nil || err.Error() != "stage is required" {
		t.Fatalf("Append() error = %v, want stage is required", err)
	}
	if err := repo.Append(ctx, ImportRunItemEvent{EventID: "evt-1", RunID: "run-1", ItemID: "item-1", EventType: "confirm", Stage: "confirm"}); err == nil || err.Error() != "message is required" {
		t.Fatalf("Append() error = %v, want message is required", err)
	}

	if err := store.ImportRuns().Create(ctx, ImportRun{
		RunID:      "run-1",
		HostID:     "host-1",
		Mode:       "strict",
		AccessMode: "subscription",
		State:      "running",
	}); err != nil {
		t.Fatalf("ImportRuns().Create() error = %v", err)
	}
	if err := store.ImportRunItems().Create(ctx, ImportRunItem{
		ItemID:              "item-1",
		RunID:               "run-1",
		BaseURL:             "https://api.example.com/v1",
		ProviderID:          "provider-1",
		APIKeyFingerprint:   "fp-1",
		CurrentStage:        "confirm",
		ConfirmationStatus:  "pending",
		AccessStatus:        "unknown",
		MatchedAccountState: "active",
		AccountResolution:   "created",
	}); err != nil {
		t.Fatalf("ImportRunItems().Create() error = %v", err)
	}

	if err := repo.Append(ctx, ImportRunItemEvent{
		EventID:   "evt-1",
		RunID:     "run-1",
		ItemID:    "item-1",
		EventType: "confirm",
		Stage:     "confirm",
		Message:   "scheduled",
	}); err != nil {
		t.Fatalf("Append() error = %v", err)
	}

	events, err := repo.ListByItemID(ctx, "item-1")
	if err != nil {
		t.Fatalf("ListByItemID() error = %v", err)
	}
	if len(events) != 1 {
		t.Fatalf("len(events) = %d, want 1", len(events))
	}
	if events[0].PayloadJSON != "{}" {
		t.Fatalf("PayloadJSON = %q, want {}", events[0].PayloadJSON)
	}
}

func TestImportRunItemEventsRepoListByItemIDValidation(t *testing.T) {
	t.Parallel()

	_, err := openTestDB(t).ImportRunEvents().ListByItemID(context.Background(), "")
	if err == nil || err.Error() != "item_id is required" {
		t.Fatalf("ListByItemID() error = %v, want item_id is required", err)
	}
}
