package integration

import (
	"os"
	"path/filepath"
	"strings"
	"testing"

	"github.com/marte-dev/marte-dev-tools/internal/index"
	"github.com/marte-dev/marte-dev-tools/internal/parser"
	"github.com/marte-dev/marte-dev-tools/internal/validator"
)

func TestProjectSpecificSchema(t *testing.T) {
	// Create temp dir
	tmpDir, err := os.MkdirTemp("", "mdt_test")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(tmpDir)

	// Define project schema
	schemaContent := `
{
  "classes": {
    "ProjectClass": {
      "fields": [
        {"name": "CustomField", "type": "int", "mandatory": true}
      ]
    }
  }
}
`
	err = os.WriteFile(filepath.Join(tmpDir, ".marte_schema.json"), []byte(schemaContent), 0644)
	if err != nil {
		t.Fatal(err)
	}

	// Define MARTe file using ProjectClass
	marteContent := `
+Obj = {
    Class = ProjectClass
    // Missing CustomField
}
`
	// We parse the content in memory, but we need the validator to look in tmpDir
	p := parser.NewParser(marteContent)
	config, err := p.Parse()
	if err != nil {
		t.Fatal(err)
	}

	idx := index.NewProjectTree()
	idx.AddFile("project.marte", config)

	// Pass tmpDir as projectRoot
	v := validator.NewValidator(idx, tmpDir)
	v.ValidateProject()

	found := false
	for _, d := range v.Diagnostics {
		if strings.Contains(d.Message, "Missing mandatory field 'CustomField'") {
			found = true
			break
		}
	}

	if !found {
		t.Error("Expected error for missing 'CustomField' defined in project schema")
	}
}
