package system

import (
	"context"
	"testing"

	"kra/internal/biz"
)

func TestAnnouncementRepositoryKeepsRawIDQuerySemantics(t *testing.T) {
	data := newTransactionTestData(t)
	repo := &announcementRepo{data: data}
	ctx := context.Background()
	item := &biz.Announcement{Title: "notice"}
	if err := repo.Create(ctx, item); err != nil {
		t.Fatal(err)
	}

	found, err := repo.Find(ctx, "1")
	if err != nil {
		t.Fatal(err)
	}
	if found.ID != item.ID {
		t.Fatalf("found announcement ID = %d, want %d", found.ID, item.ID)
	}
	if err = repo.DeleteByIDs(ctx, nil); err != nil {
		t.Fatalf("empty bulk delete must be a successful no-op: %v", err)
	}
	if err = repo.Delete(ctx, "not-a-number"); err != nil {
		t.Fatalf("raw invalid ID must be delegated to the storage query: %v", err)
	}

	var count int64
	if err = data.gormDB.WithContext(ctx).Model(&announcementPO{}).Count(&count).Error; err != nil {
		t.Fatal(err)
	}
	if count != 1 {
		t.Fatalf("raw invalid ID removed rows: count=%d", count)
	}
}

func TestAnnouncementRepositoryPreservesSignedUserID(t *testing.T) {
	data := newTransactionTestData(t)
	repo := &announcementRepo{data: data}
	ctx := context.Background()
	userID := -1
	item := &biz.Announcement{Title: "notice", UserID: &userID}
	if err := repo.Create(ctx, item); err != nil {
		t.Fatal(err)
	}

	found, err := repo.Find(ctx, "1")
	if err != nil {
		t.Fatal(err)
	}
	if found.UserID == nil || *found.UserID != userID {
		t.Fatalf("found user ID = %v, want %d", found.UserID, userID)
	}
}
