package sqlite

import (
	"context"
	"testing"
)

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

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

	if _, err := repo.Create(ctx, AccessClosureRecord{}); err == nil || err.Error() != "batch_id is required" {
		t.Fatalf("Create() error = %v, want batch_id is required", err)
	}
	if _, err := repo.Create(ctx, AccessClosureRecord{BatchID: 1}); err == nil || err.Error() != "closure_type is required" {
		t.Fatalf("Create() error = %v, want closure_type is required", err)
	}
	if _, err := repo.Create(ctx, AccessClosureRecord{BatchID: 1, ClosureType: "self_service"}); err == nil || err.Error() != "status is required" {
		t.Fatalf("Create() error = %v, want status is required", err)
	}

	batchID := createTestBatch(t, store)
	recordID, err := repo.Create(ctx, AccessClosureRecord{
		BatchID:     batchID,
		ClosureType: "self_service",
		Status:      "ready",
	})
	if err != nil {
		t.Fatalf("Create() error = %v", err)
	}
	if recordID <= 0 {
		t.Fatalf("recordID = %d, want positive id", recordID)
	}

	records, err := repo.GetByBatchID(ctx, batchID)
	if err != nil {
		t.Fatalf("GetByBatchID() error = %v", err)
	}
	if len(records) != 1 {
		t.Fatalf("len(records) = %d, want 1", len(records))
	}
	if records[0].DetailsJSON != "{}" {
		t.Fatalf("DetailsJSON = %q, want {}", records[0].DetailsJSON)
	}
}

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

	_, err := openTestDB(t).AccessClosures().GetByBatchID(context.Background(), 0)
	if err == nil || err.Error() != "batch_id is required" {
		t.Fatalf("GetByBatchID() error = %v, want batch_id is required", err)
	}
}
