package sqlite

import (
	"context"
	"testing"
)

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

	store := openTestDB(t)
	hostID := createTestHostWithBaseURL(t, store, "host-1", "https://host.example")
	packID := createTestPack(t, store)
	providerID, err := store.Providers().Create(context.Background(), Provider{
		PackID:      packID,
		ProviderID:  "provider-1",
		DisplayName: "Provider 1",
		BaseURL:     "https://provider.example",
		Platform:    "openai",
	})
	if err != nil {
		t.Fatalf("Providers().Create() error = %v", err)
	}
	batchID, err := store.ImportBatches().Create(context.Background(), ImportBatch{
		HostID:       hostID,
		PackID:       packID,
		ProviderID:   providerID,
		Mode:         "partial",
		BatchStatus:  "succeeded",
		AccessStatus: "self_service_ready",
	})
	if err != nil {
		t.Fatalf("ImportBatches().Create() error = %v", err)
	}

	if _, err := store.ReconcileRuns().Create(context.Background(), ReconcileRun{
		BatchID:     batchID,
		HostID:      hostID,
		ProviderID:  providerID,
		Status:      "active",
		SummaryJSON: `{"ok":true}`,
	}); err != nil {
		t.Fatalf("Create() error = %v", err)
	}

	runs, err := store.ReconcileRuns().GetByProviderIDAndHostID(context.Background(), providerID, hostID)
	if err != nil {
		t.Fatalf("GetByProviderIDAndHostID() error = %v", err)
	}
	if len(runs) != 1 {
		t.Fatalf("reconcile runs = %d, want 1", len(runs))
	}
	if runs[0].CreatedAt == "" {
		t.Fatal("CreatedAt = empty, want timestamp")
	}
}
