package infra

import (
	"path/filepath"
	"testing"
)

func TestScanFlowsDir(t *testing.T) {
	root := registryRoot()
	flowsDir := filepath.Join(root, "dev", "flows")

	t.Run("scan devuelve al menos 5 flows", func(t *testing.T) {
		flows, err := ScanFlowsDir(flowsDir)
		if err != nil {
			t.Fatalf("ScanFlowsDir: %v", err)
		}
		if len(flows) < 5 {
			t.Errorf("expected >= 5 flows, got %d", len(flows))
		}
	})

	t.Run("flow 0001 esta presente", func(t *testing.T) {
		flows, err := ScanFlowsDir(flowsDir)
		if err != nil {
			t.Fatalf("ScanFlowsDir: %v", err)
		}
		found := false
		for _, f := range flows {
			if f.ID == "0001" {
				found = true
				break
			}
		}
		if !found {
			t.Error("flow 0001 not found in scan results")
		}
	})

	t.Run("flows tienen FilePath y MtimeNs", func(t *testing.T) {
		flows, err := ScanFlowsDir(flowsDir)
		if err != nil {
			t.Fatalf("ScanFlowsDir: %v", err)
		}
		for _, f := range flows {
			if f.FilePath == "" {
				t.Errorf("flow %q has empty FilePath", f.ID)
			}
			if f.MtimeNs == 0 {
				t.Errorf("flow %q has zero MtimeNs", f.ID)
			}
		}
	})

	t.Run("flows ordenados por ID asc", func(t *testing.T) {
		flows, err := ScanFlowsDir(flowsDir)
		if err != nil {
			t.Fatalf("ScanFlowsDir: %v", err)
		}
		for i := 1; i < len(flows); i++ {
			if flows[i].ID < flows[i-1].ID {
				t.Errorf("not sorted at index %d: %q < %q", i, flows[i].ID, flows[i-1].ID)
				break
			}
		}
	})
}
