package spells

import (
	"bytes"
	"encoding/json"
	"strings"
	"testing"

	"github.com/santhosh-tekuri/jsonschema/v6"
)

func TestLoadResponseSchemaUsesExtractorOwnedLLMSchema(t *testing.T) {
	schema, err := loadResponseSchema()
	if err != nil {
		t.Fatalf("loadResponseSchema() error = %v, want nil", err)
	}
	if got, want := string(schema.Key), "dnd_spells_llm"; got != want {
		t.Fatalf("schema key = %q, want %q", got, want)
	}
	if got, want := schema.ID, "notarius.dnd.spells.llm"; got != want {
		t.Fatalf("schema ID = %q, want %q", got, want)
	}
	if got, want := schema.Version, "v1"; got != want {
		t.Fatalf("schema version = %q, want %q", got, want)
	}
	if got, want := schema.Name, "notarius_dnd_spells_llm_v1"; got != want {
		t.Fatalf("schema name = %q, want %q", got, want)
	}
	if !strings.HasPrefix(schema.SHA256, "sha256:") || !json.Valid(schema.JSONSchema) {
		t.Fatalf("schema metadata = %#v, want valid hashed JSON", schema)
	}
	var schemaDocument map[string]any
	if err := json.Unmarshal(schema.JSONSchema, &schemaDocument); err != nil {
		t.Fatalf("Unmarshal(schema.JSONSchema) error = %v, want nil", err)
	}
	if schemaDocument["$id"] != "notarius.dnd.spells.llm" {
		t.Fatalf("schema $id = %#v, want private LLM schema ID", schemaDocument["$id"])
	}

	valid := validSpellsResponse()
	validJSON, err := json.Marshal(valid)
	if err != nil {
		t.Fatalf("Marshal(validSpellsResponse()) error = %v, want nil", err)
	}
	if err := validateJSONSchema(validJSON, schema.JSONSchema); err != nil {
		t.Fatalf("valid private spells response rejected: %v", err)
	}
	for _, test := range []struct {
		name     string
		response map[string]any
		valid    bool
	}{
		{
			name: "semantic blanks and empty evidence",
			response: map[string]any{"spell_casts": []any{map[string]any{
				"caster": "", "spell": "", "source_refs": []any{},
			}}},
			valid: true,
		},
		{
			name: "nonpositive unit candidates",
			response: map[string]any{"spell_casts": []any{map[string]any{
				"caster": "Aria", "spell": "Cure Wounds",
				"source_refs": []any{map[string]any{"start_unit_id": 0, "end_unit_id": -1}},
			}}},
			valid: true,
		},
		{
			name: "missing required field",
			response: map[string]any{"spell_casts": []any{map[string]any{
				"spell": "Cure Wounds", "source_refs": []any{},
			}}},
		},
		{
			name: "unknown field",
			response: map[string]any{"spell_casts": []any{map[string]any{
				"caster": "Aria", "spell": "Cure Wounds", "source_refs": []any{}, "id": "assigned later",
			}}},
		},
		{
			name: "wrong field type",
			response: map[string]any{"spell_casts": []any{map[string]any{
				"caster": 7, "spell": "Cure Wounds", "source_refs": []any{},
			}}},
		},
		{
			name: "noninteger source identifier",
			response: map[string]any{"spell_casts": []any{map[string]any{
				"caster": "Aria", "spell": "Cure Wounds",
				"source_refs": []any{map[string]any{"start_unit_id": 1.5, "end_unit_id": 2}},
			}}},
		},
	} {
		t.Run(test.name, func(t *testing.T) {
			content, err := json.Marshal(test.response)
			if err != nil {
				t.Fatal(err)
			}
			err = validateJSONSchema(content, schema.JSONSchema)
			if (err == nil) != test.valid {
				t.Fatalf("validateJSONSchema() error = %v, want valid=%t", err, test.valid)
			}
		})
	}
	if err := validateJSONSchema([]byte(`{"spell_casts":`), schema.JSONSchema); err == nil {
		t.Fatal("validateJSONSchema() error = nil, want malformed JSON rejected")
	}

	withCanonicalSourceID := validSpellsResponse()
	withCanonicalSourceID["spell_casts"].([]any)[0].(map[string]any)["source_refs"].([]any)[0].(map[string]any)["source_id"] = "session-alpha"
	content, err := json.Marshal(withCanonicalSourceID)
	if err != nil {
		t.Fatalf("Marshal(response with source_id) error = %v, want nil", err)
	}
	if err := validateJSONSchema(content, schema.JSONSchema); err == nil {
		t.Fatal("validateJSONSchema() error = nil, want canonical source_id rejected")
	}
}

func TestResponseSchemaJSONIsMutationSafe(t *testing.T) {
	first, err := loadResponseSchema()
	if err != nil {
		t.Fatalf("loadResponseSchema() error = %v", err)
	}
	first.JSONSchema[0] = '['
	second, err := loadResponseSchema()
	if err != nil || !json.Valid(second.JSONSchema) || bytes.Equal(first.JSONSchema, second.JSONSchema) {
		t.Fatalf("second schema = %s, %v; want defensive valid copy", second.JSONSchema, err)
	}
}

func TestResponseSchemaDiagnosticsOmitRawSchema(t *testing.T) {
	schema, err := loadResponseSchema()
	if err != nil {
		t.Fatalf("loadResponseSchema() error = %v", err)
	}
	diagnostics := schema.DiagnosticsMap()
	if diagnostics["key"] != ResponseSchemaKey {
		t.Fatalf("diagnostics = %#v, want response key", diagnostics)
	}
	if _, ok := diagnostics["json_schema"]; ok {
		t.Fatalf("diagnostics include raw schema: %#v", diagnostics)
	}
}

func validSpellsResponse() map[string]any {
	return map[string]any{
		"spell_casts": []any{
			map[string]any{
				"caster": "Aria",
				"spell":  "Cure Wounds",
				"source_refs": []any{
					map[string]any{"start_unit_id": 1, "end_unit_id": 2},
				},
			},
		},
	}
}

func validateJSONSchema(instanceContent, schemaContent []byte) error {
	instance, err := jsonschema.UnmarshalJSON(bytes.NewReader(instanceContent))
	if err != nil {
		return err
	}
	schemaDocument, err := jsonschema.UnmarshalJSON(bytes.NewReader(schemaContent))
	if err != nil {
		return err
	}
	compiler := jsonschema.NewCompiler()
	if err := compiler.AddResource("schema.json", schemaDocument); err != nil {
		return err
	}
	schema, err := compiler.Compile("schema.json")
	if err != nil {
		return err
	}
	return schema.Validate(instance)
}
