package airports

import (
	"testing"
)

func TestNewStationNeighbors(t *testing.T) {
	sn := NewStationNeighbors("c1")
	if sn.CityCode != "c1" {
		t.Errorf("expected CityCode 'c1', got '%s'", sn.CityCode)
	}
	// Neighbors is initialized as an empty slice, not nil
	if len(sn.Neighbors) != 0 {
		t.Errorf("expected Neighbors to be an empty slice, got length %d", len(sn.Neighbors))
	}
	if sn.byID == nil {
		t.Error("expected byID map to be initialized")
	}
}

func TestStationNeighborsAdd(t *testing.T) {
	sn := NewStationNeighbors("c1")
	sn.Add("s1", "Station One", "geo")
	sn.Add("s2", "Station Two", "manual")

	if len(sn.Neighbors) != 2 {
		t.Errorf("expected 2 neighbors, got %d", len(sn.Neighbors))
	}

	// Check byID map
	if idx, ok := sn.byID["s1"]; !ok || idx != 0 {
		t.Errorf("expected s1 to be at index 0 in byID")
	}
	if idx, ok := sn.byID["s2"]; !ok || idx != 1 {
		t.Errorf("expected s2 to be at index 1 in byID")
	}
}

func TestStationNeighborsMarkExcluded(t *testing.T) {
	sn := NewStationNeighbors("c1")
	sn.Add("s1", "Station One", "geo")

	sn.MarkExcluded("s1")
	isExcluded, found := sn.IsExcluded("s1")
	if !found {
		t.Error("expected s1 to be found in byID")
	}
	if !isExcluded {
		t.Error("expected s1 to be excluded after MarkExcluded")
	}
}

func TestStationNeighborsIsExcluded(t *testing.T) {
	sn := NewStationNeighbors("c1")
	sn.Add("s1", "Station One", "geo")

	// Test existing station
	isExcluded, found := sn.IsExcluded("s1")
	if !found {
		t.Error("expected s1 to be found")
	}
	if isExcluded {
		t.Error("expected s1 to not be excluded initially")
	}

	// Test non-existing station
	isExcluded, found = sn.IsExcluded("s999")
	if found {
		t.Error("expected s999 to not be found")
	}
	if isExcluded {
		t.Error("expected isExcluded to be false for non-existing station")
	}
}

func TestStationNeighborsGet(t *testing.T) {
	sn := NewStationNeighbors("c1")
	sn.Add("s1", "Station One", "geo")

	neighbor, found := sn.Get("s1")
	if !found {
		t.Error("expected s1 to be found")
	}
	if neighbor.StationID != "s1" {
		t.Errorf("expected StationID 's1', got '%s'", neighbor.StationID)
	}
	if neighbor.Name != "Station One" {
		t.Errorf("expected Name 'Station One', got '%s'", neighbor.Name)
	}

	// Test non-existing station
	neighbor, found = sn.Get("s999")
	if found {
		t.Error("expected s999 to not be found")
	}
	if neighbor != nil {
		t.Error("expected neighbor to be nil for non-existing station")
	}
}

func TestStationNeighborsLen(t *testing.T) {
	sn := NewStationNeighbors("c1")
	if sn.Len() != 0 {
		t.Errorf("expected 0 neighbors initially, got %d", sn.Len())
	}

	sn.Add("s1", "Station One", "geo")
	if sn.Len() != 1 {
		t.Errorf("expected 1 neighbor after add, got %d", sn.Len())
	}
}

func TestStationNeighborsSort(t *testing.T) {
	sn := NewStationNeighbors("c1")
	sn.Add("s3", "Station Three", "geo")
	sn.Add("s1", "Station One", "geo")
	sn.Add("s2", "Station Two", "geo")

	sn.Sort()

	if len(sn.Neighbors) != 3 {
		t.Errorf("expected 3 neighbors, got %d", len(sn.Neighbors))
	}
	if sn.Neighbors[0].Name != "Station One" {
		t.Errorf("expected 'Station One' first, got '%s'", sn.Neighbors[0].Name)
	}
	if sn.Neighbors[1].Name != "Station Three" {
		t.Errorf("expected 'Station Three' second, got '%s'", sn.Neighbors[1].Name)
	}
	if sn.Neighbors[2].Name != "Station Two" {
		t.Errorf("expected 'Station Two' third, got '%s'", sn.Neighbors[2].Name)
	}
}

func TestStationNeighborsGetNonExcluded(t *testing.T) {
	sn := NewStationNeighbors("c1")
	sn.Add("s1", "Station One", "geo")
	sn.Add("s2", "Station Two", "manual")
	sn.MarkExcluded("s1")

	nonExcluded := sn.GetNonExcluded()
	if len(nonExcluded) != 1 {
		t.Errorf("expected 1 non-excluded neighbor, got %d", len(nonExcluded))
	}
	if nonExcluded[0].StationID != "s2" {
		t.Errorf("expected 's2' as non-excluded, got '%s'", nonExcluded[0].StationID)
	}
}
