package partitioned

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

	"direct-dev.ru/gitea/GiteaAdmin/elowdb-go/pkg/linedb"
)

const dbDir = "./data/partitioned"

func setupDB(t *testing.T) *linedb.LineDb {
	t.Helper()
	dir := filepath.Join(dbDir, t.Name())
	_ = os.RemoveAll(dir)
	if err := os.MkdirAll(dir, 0755); err != nil {
		t.Fatalf("mkdir: %v", err)
	}

	initOptions := &linedb.LineDbInitOptions{
		DBFolder: dir,
		Collections: []linedb.JSONLFileOptions{
			{
				CollectionName: "events",
				AllocSize:      512,
				IndexedFields:  []string{"id", "tenant", "type"},
			},
		},
		Partitions: []linedb.PartitionCollection{
			{
				CollectionName: "events",
				PartIDFn: func(v any) string {
					m, ok := v.(map[string]any)
					if !ok {
						return "unknown"
					}
					tenant, ok := m["tenant"].(string)
					if !ok || tenant == "" {
						return "unknown"
					}
					return tenant
				},
			},
		},
	}

	db := linedb.NewLineDb(&linedb.LineDbOptions{
		IndexStore: linedb.NewInMemoryIndexStore(),
	})
	if err := db.Init(true, initOptions); err != nil {
		t.Fatalf("Init: %v", err)
	}
	return db
}

func TestPartitioned_InsertRead(t *testing.T) {
	db := setupDB(t)
	defer db.Close()
	opts := linedb.LineDbAdapterOptions{}

	events := []any{
		map[string]any{"id": 1, "tenant": "A", "type": "signup", "status": "new", "ts": time.Now().Unix()},
		map[string]any{"id": 2, "tenant": "A", "type": "purchase", "status": "new", "ts": time.Now().Unix()},
		map[string]any{"id": 3, "tenant": "B", "type": "signup", "status": "new", "ts": time.Now().Unix()},
		map[string]any{"id": -1, "tenant": "", "type": "signup", "status": "new", "ts": time.Now().Unix()},
	}
	if err := db.Insert(events, "events", opts); err != nil {
		t.Fatalf("Insert: %v", err)
	}

	tenantA, err := db.ReadByFilter(map[string]any{"tenant": "A"}, "events", linedb.LineDbAdapterOptions{FailOnFailureIndexRead: true})
	if err != nil {
		t.Fatalf("ReadByFilter(nil): %v", err)
	}
	if len(tenantA) != 2 {
		t.Errorf("expected 2 events, got %d", len(tenantA))
	}

	all, err := db.ReadByFilter(nil, "events", linedb.LineDbAdapterOptions{FailOnFailureIndexRead: false})
	if err != nil {
		t.Fatalf("ReadByFilter(nil): %v", err)
	}
	if len(all) != 4 {
		t.Errorf("expected 4 events, got %d", len(all))
	}
}

func TestPartitioned_ReadByFilter_ByTenant(t *testing.T) {
	db := setupDB(t)
	defer db.Close()
	opts := linedb.LineDbAdapterOptions{}

	if err := db.Insert([]any{
		map[string]any{"id": 1, "tenant": "X", "type": "a"},
		map[string]any{"id": 2, "tenant": "X", "type": "b"},
		map[string]any{"id": 3, "tenant": "Y", "type": "c"},
	}, "events", opts); err != nil {
		t.Fatalf("Insert: %v", err)
	}

	tenantX, err := db.ReadByFilter(map[string]any{"tenant": "X"}, "events", opts)
	if err != nil {
		t.Fatalf("ReadByFilter tenant X: %v", err)
	}
	if len(tenantX) != 2 {
		t.Fatalf("tenant X: expected 2, got %d", len(tenantX))
	}

	tenantY, _ := db.ReadByFilter(map[string]any{"tenant": "Y"}, "events", opts)
	if len(tenantY) != 1 {
		t.Fatalf("tenant Y: expected 1, got %d", len(tenantY))
	}
}

func TestPartitioned_ReadByFilter_Indexed(t *testing.T) {
	db := setupDB(t)
	defer db.Close()
	opts := linedb.LineDbAdapterOptions{}

	if err := db.Insert([]any{
		map[string]any{"id": 100, "tenant": "T1", "type": "signup"},
		map[string]any{"id": 200, "tenant": "T1", "type": "purchase"},
	}, "events", opts); err != nil {
		t.Fatalf("Insert: %v", err)
	}

	// Поиск по индексированному полю id
	found, err := db.ReadByFilter(map[string]any{"id": 100}, "events", opts)
	if err != nil {
		t.Fatalf("ReadByFilter id: %v", err)
	}
	if len(found) != 1 {
		t.Fatalf("ReadByFilter id 100: expected 1, got %d", len(found))
	}

	// Поиск по type
	foundType, err := db.ReadByFilter(map[string]any{"type": "purchase"}, "events", linedb.LineDbAdapterOptions{FailOnFailureIndexRead: true})
	if err != nil {
		t.Fatalf("ReadByFilter type: %v", err)
	}
	if len(foundType) != 1 {
		t.Fatalf("ReadByFilter type: expected 1, got %d", len(foundType))
	}
}

func TestPartitioned_Update(t *testing.T) {
	db := setupDB(t)
	defer db.Close()
	opts := linedb.LineDbAdapterOptions{}

	if err := db.Insert([]any{
		map[string]any{"id": 1, "tenant": "A", "type": "new", "status": "pending"},
		map[string]any{"id": 2, "tenant": "A", "type": "new", "status": "pending"},
	}, "events", opts); err != nil {
		t.Fatalf("Insert: %v", err)
	}

	updated, err := db.Update(
		map[string]any{"status": "processed"},
		"events",
		map[string]any{"tenant": "A"},
		opts,
	)
	if err != nil {
		t.Fatalf("Update: %v", err)
	}
	if len(updated) != 2 {
		t.Fatalf("Update: expected 2, got %d", len(updated))
	}

	processed, _ := db.ReadByFilter(map[string]any{"tenant": "A", "status": "processed"}, "events", opts)
	if len(processed) != 2 {
		t.Fatalf("After update: expected 2 processed, got %d", len(processed))
	}
}

func TestPartitioned_Delete(t *testing.T) {
	db := setupDB(t)
	defer db.Close()
	opts := linedb.LineDbAdapterOptions{}

	if err := db.Insert([]any{
		map[string]any{"id": 1, "tenant": "P", "type": "a"},
		map[string]any{"id": 2, "tenant": "P", "type": "b"},
		map[string]any{"id": 3, "tenant": "Q", "type": "c"},
	}, "events", opts); err != nil {
		t.Fatalf("Insert: %v", err)
	}

	deleted, err := db.Delete(map[string]any{"id": 2}, "events", opts)
	if err != nil {
		t.Fatalf("Delete: %v", err)
	}
	if len(deleted) != 1 {
		t.Fatalf("Delete: expected 1, got %d", len(deleted))
	}

	all, _ := db.ReadByFilter(nil, "events", opts)
	if len(all) != 2 {
		t.Fatalf("After delete expected 2, got %d", len(all))
	}
}

func TestPartitioned_WriteWithDoIndexing(t *testing.T) {
	db := setupDB(t)
	defer db.Close()
	opts := linedb.LineDbAdapterOptions{}

	if err := db.Write(
		[]any{map[string]any{"id": 999, "tenant": "W", "type": "direct"}},
		"events",
		linedb.LineDbAdapterOptions{DoIndexing: true},
	); err != nil {
		t.Fatalf("Write: %v", err)
	}

	found, _ := db.ReadByFilter(map[string]any{"id": 999}, "events", opts)
	if len(found) != 1 {
		t.Fatalf("Write+DoIndexing: expected 1, got %d", len(found))
	}
}

func TestPartitioned_FullCycle(t *testing.T) {
	db := setupDB(t)
	defer db.Close()
	opts := linedb.LineDbAdapterOptions{}

	// 1. Insert в разные партиции
	if err := db.Insert([]any{
		map[string]any{"id": 1, "tenant": "Alpha", "type": "signup"},
		map[string]any{"id": 2, "tenant": "Alpha", "type": "login"},
		map[string]any{"id": 3, "tenant": "Beta", "type": "signup"},
	}, "events", opts); err != nil {
		t.Fatalf("Insert: %v", err)
	}

	// 2. Чтение
	alpha, _ := db.ReadByFilter(map[string]any{"tenant": "Alpha"}, "events", opts)
	if len(alpha) != 2 {
		t.Fatalf("Alpha: expected 2, got %d", len(alpha))
	}

	// 3. Update в одной партиции
	_, err := db.Update(
		map[string]any{"type": "updated"},
		"events",
		map[string]any{"id": 2},
		opts,
	)
	if err != nil {
		t.Fatalf("Update: %v", err)
	}

	// 4. Delete
	_, err = db.Delete(map[string]any{"id": 3}, "events", opts)
	if err != nil {
		t.Fatalf("Delete: %v", err)
	}

	// 5. Итог
	all, _ := db.ReadByFilter(nil, "events", opts)
	if len(all) != 2 {
		t.Fatalf("After cycle expected 2, got %d", len(all))
	}
	updated, _ := db.ReadByFilter(map[string]any{"id": 2}, "events", opts)
	if len(updated) != 1 {
		t.Fatalf("id 2: expected 1, got %d", len(updated))
	}
	if m, ok := updated[0].(map[string]any); ok && m["type"] != "updated" {
		t.Errorf("Update did not apply: type=%v", m["type"])
	}
}
