package dbsync

import (
	"context"
	"path/filepath"
	"testing"
	"time"
)

func TestRollbackRejectsNonApplied(t *testing.T) {
	dir := t.TempDir()
	st, err := NewFileStore(filepath.Join(dir, "dbsync"))
	if err != nil {
		t.Fatal(err)
	}
	o := LwwOverride{
		ID:        "ov1",
		TenantID:  1,
		ChannelID: "ch1",
		Table:     "orders",
		RowPK:     "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee",
		Outcome:   OutcomeKept,
		CreatedAt: time.Now().UTC(),
	}
	if err := st.AddLwwOverride(o); err != nil {
		t.Fatal(err)
	}
	_, err = RollbackLwwOverride(context.Background(), st, "ov1")
	if err == nil {
		t.Fatal("expected reject kept_target")
	}
}

func TestGetLwwOverride(t *testing.T) {
	dir := t.TempDir()
	st, err := NewFileStore(filepath.Join(dir, "dbsync"))
	if err != nil {
		t.Fatal(err)
	}
	o := LwwOverride{
		ID:       "ov2",
		TenantID: 1,
		Table:    "t",
		RowPK:    "pk",
		Outcome:  OutcomeApplied,
	}
	if err := st.AddLwwOverride(o); err != nil {
		t.Fatal(err)
	}
	got, err := st.GetLwwOverride("ov2")
	if err != nil || got.Table != "t" {
		t.Fatalf("got=%+v err=%v", got, err)
	}
}
