package store

import (
	"errors"
	"testing"
)

func TestCreateAndGetWorkload(t *testing.T) {
	s := newTestStore(t)

	w, err := s.CreateWorkload(Workload{
		Kind: string(WorkloadKindProject), RefID: "proj-1", Name: "test-workload",
	})
	if err != nil {
		t.Fatalf("CreateWorkload: %v", err)
	}
	if w.ID == "" {
		t.Fatal("workload ID should be set")
	}

	got, err := s.GetWorkloadByID(w.ID)
	if err != nil {
		t.Fatalf("GetWorkloadByID: %v", err)
	}
	if got.Name != "test-workload" || got.Kind != "project" || got.RefID != "proj-1" {
		t.Fatalf("got %+v, want kind=project ref=proj-1 name=test-workload", got)
	}
}

func TestGetWorkloadByRef(t *testing.T) {
	s := newTestStore(t)

	_, err := s.CreateWorkload(Workload{
		Kind: string(WorkloadKindStack), RefID: "stack-42", Name: "compose-app",
	})
	if err != nil {
		t.Fatalf("CreateWorkload: %v", err)
	}

	got, err := s.GetWorkloadByRef(WorkloadKindStack, "stack-42")
	if err != nil {
		t.Fatalf("GetWorkloadByRef: %v", err)
	}
	if got.Name != "compose-app" {
		t.Fatalf("got name %q, want compose-app", got.Name)
	}

	if _, err := s.GetWorkloadByRef(WorkloadKindProject, "stack-42"); !errors.Is(err, ErrNotFound) {
		t.Fatalf("wrong-kind lookup should be NotFound, got %v", err)
	}
}

func TestUniqueKindRefID(t *testing.T) {
	s := newTestStore(t)

	if _, err := s.CreateWorkload(Workload{Kind: "project", RefID: "p1", Name: "a"}); err != nil {
		t.Fatalf("first insert: %v", err)
	}
	if _, err := s.CreateWorkload(Workload{Kind: "project", RefID: "p1", Name: "dupe"}); err == nil {
		t.Fatal("expected UNIQUE(kind, ref_id) violation, got nil")
	}
}

func TestUpdateWorkload(t *testing.T) {
	s := newTestStore(t)

	w, _ := s.CreateWorkload(Workload{Kind: "project", RefID: "p1", Name: "original"})
	w.Name = "renamed"
	w.AppID = "app-1"
	w.WebhookRequireSignature = true
	if err := s.UpdateWorkload(w); err != nil {
		t.Fatalf("UpdateWorkload: %v", err)
	}

	got, _ := s.GetWorkloadByID(w.ID)
	if got.Name != "renamed" {
		t.Fatalf("name not updated: got %q", got.Name)
	}
	if got.AppID != "app-1" {
		t.Fatalf("app_id not updated: got %q", got.AppID)
	}
	if !got.WebhookRequireSignature {
		t.Fatalf("webhook_require_signature not updated")
	}
}

// GetWorkloadByWebhookSecret was deleted with the legacy
// `/api/webhook/workloads/{secret}` route in the hard cutover; the
// inbound webhook surface is now first-class Triggers.

func TestListWorkloads(t *testing.T) {
	s := newTestStore(t)

	s.CreateWorkload(Workload{Kind: "project", RefID: "p1", Name: "alpha"})
	s.CreateWorkload(Workload{Kind: "stack", RefID: "s1", Name: "bravo"})
	s.CreateWorkload(Workload{Kind: "project", RefID: "p2", Name: "charlie"})

	all, err := s.ListWorkloads("")
	if err != nil {
		t.Fatalf("ListWorkloads all: %v", err)
	}
	if len(all) != 3 {
		t.Fatalf("expected 3 workloads, got %d", len(all))
	}

	projects, err := s.ListWorkloads(WorkloadKindProject)
	if err != nil {
		t.Fatalf("ListWorkloads project: %v", err)
	}
	if len(projects) != 2 {
		t.Fatalf("expected 2 project workloads, got %d", len(projects))
	}
}

func TestDeleteWorkloadByRef(t *testing.T) {
	s := newTestStore(t)

	w, _ := s.CreateWorkload(Workload{Kind: "project", RefID: "p1", Name: "n"})

	if err := s.DeleteWorkloadByRef(WorkloadKindProject, "p1"); err != nil {
		t.Fatalf("DeleteWorkloadByRef: %v", err)
	}
	if _, err := s.GetWorkloadByID(w.ID); !errors.Is(err, ErrNotFound) {
		t.Fatalf("expected NotFound after delete, got %v", err)
	}

	// Idempotent — deleting non-existent ref is fine.
	if err := s.DeleteWorkloadByRef(WorkloadKindProject, "ghost"); err != nil {
		t.Fatalf("DeleteWorkloadByRef should be idempotent: %v", err)
	}
}
