package parser_test

import (
	"context"
	"testing"

	"bit.admedia.com/scm/ad/adpilot-indexing-code-parser.com/internal/service/parser"
	"bit.admedia.com/scm/ad/adpilot-indexing-code-parser.com/internal/service/parser/registry"
	"bit.admedia.com/scm/ad/adpilot-indexing-code-parser.com/internal/store"
)

func TestMixedLanguagePipeline(t *testing.T) {
	workspace := setupWorkspace(t, map[string]string{
		"main.go":       "package main\nfunc main() {}\n",
		"app.py":        "def handler():\n    return 1\n",
		"index.html":    "<html><body>hi</body></html>\n",
		"styles/app.css": ".app { color: black; }\n",
	})
	artifactDir := t.TempDir()
	fs, err := store.NewFilesystemStore(artifactDir)
	if err != nil {
		t.Fatalf("NewFilesystemStore: %v", err)
	}

	built := registry.Build(registry.Options{EnableFallback: true})
	pub := &mockPublisher{}
	svc := parser.NewService(parser.ServiceConfig{
		WorkspacePath: workspace,
		Workers:       4,
	}, built.Analyzers, pub, fs, parser.WithFallbackAnalyzer(built.Fallback))

	events := []struct {
		path     string
		language string
	}{
		{"main.go", "go"},
		{"app.py", "python"},
		{"index.html", "html"},
		{"styles/app.css", "css"},
	}
	for _, tc := range events {
		event := fileChangedEvent("snap_mix", tc.path)
		event.Language = tc.language
		if err := svc.ProcessFileChanged(context.Background(), event); err != nil {
			t.Fatalf("ProcessFileChanged(%s): %v", tc.path, err)
		}
		job, ok := svc.GetParseJob(parser.JobIDFromEvent(event))
		if !ok || job.Status != parser.JobStatusCompleted {
			t.Fatalf("job for %s not completed: %+v", tc.path, job)
		}
	}
}
