package localdb

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

func TestGetLatestLocalPricelistBySource_SkipsPricelistWithoutItems(t *testing.T) {
	local, err := New(filepath.Join(t.TempDir(), "latest_without_items.db"))
	if err != nil {
		t.Fatalf("open localdb: %v", err)
	}
	t.Cleanup(func() { _ = local.Close() })

	base := time.Now().Add(-time.Minute)
	withItems := &LocalPricelist{
		ServerID:  1001,
		Source:    "estimate",
		Version:   "E-1",
		Name:      "with-items",
		CreatedAt: base,
		SyncedAt:  base,
	}
	if err := local.SaveLocalPricelist(withItems); err != nil {
		t.Fatalf("save pricelist with items: %v", err)
	}
	storedWithItems, err := local.GetLocalPricelistByServerID(withItems.ServerID)
	if err != nil {
		t.Fatalf("load pricelist with items: %v", err)
	}
	if err := local.SaveLocalPricelistItems([]LocalPricelistItem{
		{
			PricelistID: storedWithItems.ID,
			LotName:     "CPU_A",
			Price:       100,
		},
	}); err != nil {
		t.Fatalf("save pricelist items: %v", err)
	}

	withoutItems := &LocalPricelist{
		ServerID:  1002,
		Source:    "estimate",
		Version:   "E-2",
		Name:      "without-items",
		CreatedAt: base.Add(2 * time.Second),
		SyncedAt:  base.Add(2 * time.Second),
	}
	if err := local.SaveLocalPricelist(withoutItems); err != nil {
		t.Fatalf("save pricelist without items: %v", err)
	}

	got, err := local.GetLatestLocalPricelistBySource("estimate")
	if err != nil {
		t.Fatalf("GetLatestLocalPricelistBySource: %v", err)
	}
	if got.ServerID != withItems.ServerID {
		t.Fatalf("expected server_id=%d, got %d", withItems.ServerID, got.ServerID)
	}
}

func TestGetLatestLocalPricelistBySource_TieBreaksByID(t *testing.T) {
	local, err := New(filepath.Join(t.TempDir(), "latest_tie_break.db"))
	if err != nil {
		t.Fatalf("open localdb: %v", err)
	}
	t.Cleanup(func() { _ = local.Close() })

	base := time.Now().Add(-time.Minute)
	first := &LocalPricelist{
		ServerID:  2001,
		Source:    "warehouse",
		Version:   "S-1",
		Name:      "first",
		CreatedAt: base,
		SyncedAt:  base,
	}
	if err := local.SaveLocalPricelist(first); err != nil {
		t.Fatalf("save first pricelist: %v", err)
	}
	storedFirst, err := local.GetLocalPricelistByServerID(first.ServerID)
	if err != nil {
		t.Fatalf("load first pricelist: %v", err)
	}
	if err := local.SaveLocalPricelistItems([]LocalPricelistItem{
		{
			PricelistID: storedFirst.ID,
			LotName:     "CPU_A",
			Price:       101,
		},
	}); err != nil {
		t.Fatalf("save first items: %v", err)
	}

	second := &LocalPricelist{
		ServerID:  2002,
		Source:    "warehouse",
		Version:   "S-2",
		Name:      "second",
		CreatedAt: base,
		SyncedAt:  base,
	}
	if err := local.SaveLocalPricelist(second); err != nil {
		t.Fatalf("save second pricelist: %v", err)
	}
	storedSecond, err := local.GetLocalPricelistByServerID(second.ServerID)
	if err != nil {
		t.Fatalf("load second pricelist: %v", err)
	}
	if err := local.SaveLocalPricelistItems([]LocalPricelistItem{
		{
			PricelistID: storedSecond.ID,
			LotName:     "CPU_A",
			Price:       102,
		},
	}); err != nil {
		t.Fatalf("save second items: %v", err)
	}

	got, err := local.GetLatestLocalPricelistBySource("warehouse")
	if err != nil {
		t.Fatalf("GetLatestLocalPricelistBySource: %v", err)
	}
	if got.ServerID != second.ServerID {
		t.Fatalf("expected server_id=%d, got %d", second.ServerID, got.ServerID)
	}
}
