package javascript_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/javascript"
)

const reactSource = `
import React from 'react';

export function HomePage() {
  return <div>Hello</div>;
}
`

const nextSource = `export default function Page() { return null }`

func TestAnalyzerExtractsReactComponent(t *testing.T) {
	a := javascript.NewAnalyzer(detectors.DefaultRegistry())
	artifact, chunks, err := a.ParseFile(context.Background(), []byte(reactSource), "components/HomePage.tsx", "typescript")
	if err != nil {
		t.Fatalf("ParseFile: %v", err)
	}
	if len(chunks) == 0 {
		t.Fatal("expected chunks")
	}
	foundComponent := false
	for _, node := range artifact.Nodes {
		if node.Name == "HomePage" && node.Metadata != nil && node.Metadata["framework"] == "react" {
			foundComponent = true
		}
	}
	if !foundComponent {
		t.Fatal("expected react component node")
	}
}

func TestAnalyzerDetectsNextRoute(t *testing.T) {
	a := javascript.NewAnalyzer(detectors.DefaultRegistry())
	artifact, _, err := a.ParseFile(context.Background(), []byte(nextSource), "app/dashboard/page.tsx", "typescript")
	if err != nil {
		t.Fatalf("ParseFile: %v", err)
	}
	foundRoute := false
	for _, node := range artifact.Nodes {
		if node.Metadata != nil && node.Metadata["route"] == "/dashboard" {
			foundRoute = true
		}
	}
	if !foundRoute {
		t.Fatal("expected nextjs route node")
	}
}
