package sync_test

import (
	"testing"
	"time"

	"git.mchus.pro/mchus/quoteforge/internal/localdb"
	"git.mchus.pro/mchus/quoteforge/internal/models"
	syncsvc "git.mchus.pro/mchus/quoteforge/internal/services/sync"
)

func TestSyncPricelists_BackfillsLotCategoryForUsedPricelistItems(t *testing.T) {
	local := newLocalDBForSyncTest(t)
	serverDB := newServerDBForSyncTest(t)

	if err := serverDB.AutoMigrate(
		&models.Pricelist{},
		&models.PricelistItem{},
		&models.Lot{},
	); err != nil {
		t.Fatalf("migrate server tables: %v", err)
	}

	serverPL := models.Pricelist{
		Source:       "estimate",
		Version:      "2026-02-11-001",
		Notification: "server",
		CreatedBy:    "tester",
		IsActive:     true,
		CreatedAt:    time.Now().Add(-1 * time.Hour),
	}
	if err := serverDB.Create(&serverPL).Error; err != nil {
		t.Fatalf("create server pricelist: %v", err)
	}
	if err := serverDB.Create(&models.PricelistItem{
		PricelistID:  serverPL.ID,
		LotName:      "CPU_A",
		LotCategory:  "CPU",
		Price:        10,
		PriceMethod:  "",
		MetaPrices:   "",
		ManualPrice:  nil,
		AvailableQty: nil,
	}).Error; err != nil {
		t.Fatalf("create server pricelist item: %v", err)
	}

	if err := local.SaveLocalPricelist(&localdb.LocalPricelist{
		ServerID:  serverPL.ID,
		Source:    serverPL.Source,
		Version:   serverPL.Version,
		Name:      serverPL.Notification,
		CreatedAt: serverPL.CreatedAt,
		SyncedAt:  time.Now(),
		IsUsed:    false,
	}); err != nil {
		t.Fatalf("seed local pricelist: %v", err)
	}
	localPL, err := local.GetLocalPricelistByServerID(serverPL.ID)
	if err != nil {
		t.Fatalf("get local pricelist: %v", err)
	}

	if err := local.SaveLocalPricelistItems([]localdb.LocalPricelistItem{
		{
			PricelistID: localPL.ID,
			LotName:     "CPU_A",
			LotCategory: "",
			Price:       10,
		},
	}); err != nil {
		t.Fatalf("seed local pricelist items: %v", err)
	}

	if err := local.SaveConfiguration(&localdb.LocalConfiguration{
		UUID:             "cfg-1",
		OriginalUsername: "tester",
		Name:             "cfg",
		Items:            localdb.LocalConfigItems{{LotName: "CPU_A", Quantity: 1, UnitPrice: 10}},
		IsActive:         true,
		PricelistID:      &serverPL.ID,
		SyncStatus:       "synced",
		CreatedAt:        time.Now().Add(-30 * time.Minute),
		UpdatedAt:        time.Now().Add(-30 * time.Minute),
	}); err != nil {
		t.Fatalf("seed local configuration with pricelist ref: %v", err)
	}

	svc := syncsvc.NewServiceWithDB(serverDB, local)
	if _, err := svc.SyncPricelists(); err != nil {
		t.Fatalf("sync pricelists: %v", err)
	}

	items, err := local.GetLocalPricelistItems(localPL.ID)
	if err != nil {
		t.Fatalf("load local items: %v", err)
	}
	if len(items) != 1 {
		t.Fatalf("expected 1 local item, got %d", len(items))
	}
	if items[0].LotCategory != "CPU" {
		t.Fatalf("expected lot_category backfilled to CPU, got %q", items[0].LotCategory)
	}
}
