package localdb

import (
	"testing"

	"git.mchus.pro/mchus/quoteforge/internal/models"
)

func TestConfigurationConvertersPreserveBusinessFields(t *testing.T) {
	estimateID := uint(11)
	warehouseID := uint(22)
	competitorID := uint(33)

	cfg := &models.Configuration{
		UUID:                  "cfg-1",
		OwnerUsername:         "tester",
		Name:                  "Config",
		PricelistID:           &estimateID,
		WarehousePricelistID:  &warehouseID,
		CompetitorPricelistID: &competitorID,
		DisablePriceRefresh:   true,
		OnlyInStock:           true,
	}

	local := ConfigurationToLocal(cfg)
	if local.WarehousePricelistID == nil || *local.WarehousePricelistID != warehouseID {
		t.Fatalf("warehouse pricelist lost in ConfigurationToLocal: %+v", local.WarehousePricelistID)
	}
	if local.CompetitorPricelistID == nil || *local.CompetitorPricelistID != competitorID {
		t.Fatalf("competitor pricelist lost in ConfigurationToLocal: %+v", local.CompetitorPricelistID)
	}
	if !local.DisablePriceRefresh {
		t.Fatalf("disable_price_refresh lost in ConfigurationToLocal")
	}

	back := LocalToConfiguration(local)
	if back.WarehousePricelistID == nil || *back.WarehousePricelistID != warehouseID {
		t.Fatalf("warehouse pricelist lost in LocalToConfiguration: %+v", back.WarehousePricelistID)
	}
	if back.CompetitorPricelistID == nil || *back.CompetitorPricelistID != competitorID {
		t.Fatalf("competitor pricelist lost in LocalToConfiguration: %+v", back.CompetitorPricelistID)
	}
	if !back.DisablePriceRefresh {
		t.Fatalf("disable_price_refresh lost in LocalToConfiguration")
	}
}

func TestConfigurationSnapshotPreservesBusinessFields(t *testing.T) {
	estimateID := uint(11)
	warehouseID := uint(22)
	competitorID := uint(33)

	cfg := &LocalConfiguration{
		UUID:                  "cfg-1",
		Name:                  "Config",
		PricelistID:           &estimateID,
		WarehousePricelistID:  &warehouseID,
		CompetitorPricelistID: &competitorID,
		DisablePriceRefresh:   true,
		OnlyInStock:           true,
		VendorSpec: VendorSpec{
			{
				SortOrder:        10,
				VendorPartnumber: "PN-1",
				Quantity:         1,
				LotMappings: []VendorSpecLotMapping{
					{LotName: "LOT_A", QuantityPerPN: 2},
				},
			},
		},
	}

	raw, err := BuildConfigurationSnapshot(cfg)
	if err != nil {
		t.Fatalf("BuildConfigurationSnapshot: %v", err)
	}

	decoded, err := DecodeConfigurationSnapshot(raw)
	if err != nil {
		t.Fatalf("DecodeConfigurationSnapshot: %v", err)
	}
	if decoded.WarehousePricelistID == nil || *decoded.WarehousePricelistID != warehouseID {
		t.Fatalf("warehouse pricelist lost in snapshot: %+v", decoded.WarehousePricelistID)
	}
	if decoded.CompetitorPricelistID == nil || *decoded.CompetitorPricelistID != competitorID {
		t.Fatalf("competitor pricelist lost in snapshot: %+v", decoded.CompetitorPricelistID)
	}
	if !decoded.DisablePriceRefresh {
		t.Fatalf("disable_price_refresh lost in snapshot")
	}
	if len(decoded.VendorSpec) != 1 || decoded.VendorSpec[0].VendorPartnumber != "PN-1" {
		t.Fatalf("vendor_spec lost in snapshot: %+v", decoded.VendorSpec)
	}
	if len(decoded.VendorSpec[0].LotMappings) != 1 || decoded.VendorSpec[0].LotMappings[0].LotName != "LOT_A" {
		t.Fatalf("lot mappings lost in snapshot: %+v", decoded.VendorSpec)
	}
}

func TestConfigurationFingerprintIncludesPricingSelectorsAndVendorSpec(t *testing.T) {
	estimateID := uint(11)
	warehouseID := uint(22)
	competitorID := uint(33)

	base := &LocalConfiguration{
		UUID:                  "cfg-1",
		Name:                  "Config",
		ServerCount:           1,
		Items:                 LocalConfigItems{{LotName: "LOT_A", Quantity: 1, UnitPrice: 100}},
		PricelistID:           &estimateID,
		WarehousePricelistID:  &warehouseID,
		CompetitorPricelistID: &competitorID,
		DisablePriceRefresh:   true,
		OnlyInStock:           true,
		VendorSpec: VendorSpec{
			{
				SortOrder:        10,
				VendorPartnumber: "PN-1",
				Quantity:         1,
			},
		},
	}

	baseFingerprint, err := BuildConfigurationSpecPriceFingerprint(base)
	if err != nil {
		t.Fatalf("base fingerprint: %v", err)
	}

	changedPricelist := *base
	newEstimateID := uint(44)
	changedPricelist.PricelistID = &newEstimateID
	pricelistFingerprint, err := BuildConfigurationSpecPriceFingerprint(&changedPricelist)
	if err != nil {
		t.Fatalf("pricelist fingerprint: %v", err)
	}
	if pricelistFingerprint == baseFingerprint {
		t.Fatalf("expected pricelist selector to affect fingerprint")
	}

	changedVendorSpec := *base
	changedVendorSpec.VendorSpec = VendorSpec{
		{
			SortOrder:        10,
			VendorPartnumber: "PN-2",
			Quantity:         1,
		},
	}
	vendorFingerprint, err := BuildConfigurationSpecPriceFingerprint(&changedVendorSpec)
	if err != nil {
		t.Fatalf("vendor fingerprint: %v", err)
	}
	if vendorFingerprint == baseFingerprint {
		t.Fatalf("expected vendor spec to affect fingerprint")
	}
}
