package httpapi

import (
	"testing"
	"time"

	"productier/apps/backend/internal/store"
)

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

	params, err := parseActivityListParams("", "", "")
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if params.Limit != defaultActivityLimit {
		t.Fatalf("default limit = %d, want %d", params.Limit, defaultActivityLimit)
	}

	params, err = parseActivityListParams("12", "task", "foo")
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if params.Limit != 12 || params.Type != "task" || params.Query != "foo" {
		t.Fatalf("unexpected parsed params: %+v", params)
	}

	if _, err := parseActivityListParams("0", "", ""); err == nil {
		t.Fatal("expected error for out-of-range limit")
	}
	if _, err := parseActivityListParams("abc", "", ""); err == nil {
		t.Fatal("expected error for non-numeric limit")
	}
	if _, err := parseActivityListParams("5", "unknown", ""); err == nil {
		t.Fatal("expected error for invalid activity type")
	}
}

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

	entries := []store.ActivityEntry{
		{ID: "1", Title: "Task created", Detail: "Write docs", CreatedAt: time.Now().Add(-1 * time.Hour)},
		{ID: "2", Title: "Invite accepted", Detail: "alex@example.com joined", CreatedAt: time.Now().Add(-2 * time.Hour)},
		{ID: "3", Title: "Mail synced", Detail: "Inbox updated", CreatedAt: time.Now().Add(-3 * time.Hour)},
	}

	filtered := filterActivityEntries(entries, activityListParams{
		Limit: 5,
		Type:  "invite",
	})
	if len(filtered) != 1 || filtered[0].ID != "2" {
		t.Fatalf("expected invite entry only, got %+v", filtered)
	}

	filtered = filterActivityEntries(entries, activityListParams{
		Limit: 5,
		Query: "docs",
	})
	if len(filtered) != 1 || filtered[0].ID != "1" {
		t.Fatalf("expected docs match only, got %+v", filtered)
	}

	filtered = filterActivityEntries(entries, activityListParams{
		Limit: 2,
	})
	if len(filtered) != 2 {
		t.Fatalf("expected two entries due to limit, got %d", len(filtered))
	}
}

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

	cases := []struct {
		entry store.ActivityEntry
		want  string
	}{
		{entry: store.ActivityEntry{Title: "Task updated", Detail: "Done"}, want: "task"},
		{entry: store.ActivityEntry{Title: "Board group added", Detail: "Inbox"}, want: "board"},
		{entry: store.ActivityEntry{Title: "Event moved", Detail: "Calendar item"}, want: "calendar"},
		{entry: store.ActivityEntry{Title: "Note saved", Detail: "Draft"}, want: "note"},
		{entry: store.ActivityEntry{Title: "Focus started", Detail: "Pomodoro"}, want: "focus"},
		{entry: store.ActivityEntry{Title: "Mail sent", Detail: "SMTP ok"}, want: "mail"},
		{entry: store.ActivityEntry{Title: "Invite revoked", Detail: "guest"}, want: "invite"},
		{entry: store.ActivityEntry{Title: "Workspace synced", Detail: "ok"}, want: "system"},
	}

	for _, test := range cases {
		got := classifyActivityEntry(test.entry)
		if got != test.want {
			t.Fatalf("classifyActivityEntry(%q) = %q, want %q", test.entry.Title, got, test.want)
		}
	}
}
