package tools

import (
	"testing"

	"skraak/utils"
)

func TestValidateSegmentImportInput(t *testing.T) {
	t.Run("invalid dataset ID - too short", func(t *testing.T) {
		input := ImportSegmentsInput{
			DatasetID: "abc",
		}
		err := validateSegmentImportInput(input)
		if err == nil {
			t.Fatal("expected error for short dataset ID")
		}
	})

	t.Run("invalid dataset ID - too long", func(t *testing.T) {
		input := ImportSegmentsInput{
			DatasetID: "abc123def456ghi789",
		}
		err := validateSegmentImportInput(input)
		if err == nil {
			t.Fatal("expected error for long dataset ID")
		}
	})

	t.Run("invalid dataset ID - invalid characters", func(t *testing.T) {
		input := ImportSegmentsInput{
			DatasetID: "abc123!!!456",
		}
		err := validateSegmentImportInput(input)
		if err == nil {
			t.Fatal("expected error for invalid characters in dataset ID")
		}
	})

	t.Run("invalid location ID", func(t *testing.T) {
		input := ImportSegmentsInput{
			DatasetID:  "abc123def456",
			LocationID: "invalid",
		}
		err := validateSegmentImportInput(input)
		if err == nil {
			t.Fatal("expected error for invalid location ID")
		}
	})

	t.Run("invalid cluster ID", func(t *testing.T) {
		input := ImportSegmentsInput{
			DatasetID:  "abc123def456",
			LocationID: "xyz789uvw012",
			ClusterID:  "invalid",
		}
		err := validateSegmentImportInput(input)
		if err == nil {
			t.Fatal("expected error for invalid cluster ID")
		}
	})
}

func TestCountTotalSegments(t *testing.T) {
	t.Run("empty", func(t *testing.T) {
		count := countTotalSegments(map[string]scannedDataFile{})
		if count != 0 {
			t.Errorf("expected 0, got %d", count)
		}
	})

	t.Run("single file - no segments", func(t *testing.T) {
		files := map[string]scannedDataFile{
			"file1": {Segments: []*utils.Segment{}},
		}
		count := countTotalSegments(files)
		if count != 0 {
			t.Errorf("expected 0, got %d", count)
		}
	})

	t.Run("single file - multiple segments", func(t *testing.T) {
		files := map[string]scannedDataFile{
			"file1": {Segments: []*utils.Segment{{}, {}, {}}},
		}
		count := countTotalSegments(files)
		if count != 3 {
			t.Errorf("expected 3, got %d", count)
		}
	})

	t.Run("multiple files", func(t *testing.T) {
		files := map[string]scannedDataFile{
			"file1": {Segments: []*utils.Segment{{}, {}}},
			"file2": {Segments: []*utils.Segment{{}}},
			"file3": {Segments: []*utils.Segment{{}, {}, {}, {}}},
		}
		count := countTotalSegments(files)
		if count != 7 {
			t.Errorf("expected 7, got %d", count)
		}
	})
}