package php_test

import (
	"context"
	"testing"

	"bit.admedia.com/scm/ad/adpilot-indexing-code-parser.com/internal/service/parser/analyzers/detectors"
	"bit.admedia.com/scm/ad/adpilot-indexing-code-parser.com/internal/service/parser/analyzers/php"
)

const laravelSource = `
use Illuminate\Support\Facades\Route;
Route::get('/users', [UserController::class, 'index']);
`

const wpSource = `add_action('init', 'my_plugin_init');`

func TestAnalyzerDetectsLaravelRoute(t *testing.T) {
	a := php.NewAnalyzer(detectors.DefaultRegistry())
	artifact, _, err := a.ParseFile(context.Background(), []byte(laravelSource), "routes/web.php", "php")
	if err != nil {
		t.Fatalf("ParseFile: %v", err)
	}
	found := false
	for _, node := range artifact.Nodes {
		if node.Metadata != nil && node.Metadata["route"] == "/users" && node.Metadata["framework"] == "laravel" {
			found = true
		}
	}
	if !found {
		t.Fatal("expected laravel route node")
	}
}

func TestAnalyzerDetectsWordPressHook(t *testing.T) {
	a := php.NewAnalyzer(detectors.DefaultRegistry())
	artifact, _, err := a.ParseFile(context.Background(), []byte(wpSource), "plugin.php", "php")
	if err != nil {
		t.Fatalf("ParseFile: %v", err)
	}
	found := false
	for _, node := range artifact.Nodes {
		if node.Metadata != nil && node.Metadata["route"] == "init" && node.Metadata["framework"] == "wordpress" {
			found = true
		}
	}
	if !found {
		t.Fatal("expected wordpress hook node")
	}
}
