package combatturns

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

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

	"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
)

func TestLoadResponseSchemaUsesPrivateCombatShape(t *testing.T) {
	schema, err := loadResponseSchema()
	if err != nil {
		t.Fatalf("loadResponseSchema() error = %v", err)
	}
	if schema.Key != ResponseSchemaKey || schema.ID != ResponseSchemaID || schema.Version != SchemaVersion || schema.Name != ResponseSchemaName || !strings.HasPrefix(schema.SHA256, "sha256:") || !json.Valid(schema.JSONSchema) {
		t.Fatalf("schema metadata = %#v, want private combat schema identity", schema)
	}
	valid := validCombatResponse()
	content, err := json.Marshal(valid)
	if err != nil {
		t.Fatal(err)
	}
	if err := validateJSONSchema(content, schema.JSONSchema); err != nil {
		t.Fatalf("valid combat response rejected: %v", err)
	}

	withSourceID := validCombatResponse()
	withSourceID["combat_turns"].([]any)[0].(map[string]any)["source_refs"].([]any)[0].(map[string]any)["source_id"] = "session-alpha"
	content, err = json.Marshal(withSourceID)
	if err != nil {
		t.Fatal(err)
	}
	if err := validateJSONSchema(content, schema.JSONSchema); err == nil {
		t.Fatal("response schema accepted source_id, want private source-reference shape")
	}
}

func TestResponseSchemaLeavesNonCategoricalSemanticsToDeterministicValidators(t *testing.T) {
	schema, err := loadResponseSchema()
	if err != nil {
		t.Fatal(err)
	}
	semanticCandidate := validCombatResponse()
	turn := semanticCandidate["combat_turns"].([]any)[0].(map[string]any)
	turn["actor"] = ""
	ref := turn["source_refs"].([]any)[0].(map[string]any)
	ref["start_unit_id"] = 0
	ref["end_unit_id"] = -1
	content, err := json.Marshal(semanticCandidate)
	if err != nil {
		t.Fatal(err)
	}
	if err := validateJSONSchema(content, schema.JSONSchema); err != nil {
		t.Fatalf("private schema rejected non-categorical validator-owned semantics: %v", err)
	}
	turn["source_refs"] = []any{}
	content, err = json.Marshal(semanticCandidate)
	if err != nil {
		t.Fatal(err)
	}
	if err := validateJSONSchema(content, schema.JSONSchema); err != nil {
		t.Fatalf("private schema rejected empty validator-owned collections: %v", err)
	}
}

func TestResponseSchemaRetainsStructuralBoundary(t *testing.T) {
	schema, err := loadResponseSchema()
	if err != nil {
		t.Fatal(err)
	}
	for _, test := range []struct {
		name   string
		mutate func(map[string]any)
	}{
		{name: "missing actor", mutate: func(turn map[string]any) { delete(turn, "actor") }},
		{name: "wrong actor type", mutate: func(turn map[string]any) { turn["actor"] = 1 }},
		{name: "unknown field", mutate: func(turn map[string]any) { turn["unexpected"] = true }},
		{name: "missing source refs", mutate: func(turn map[string]any) { delete(turn, "source_refs") }},
		{name: "unsupported turn kind", mutate: func(turn map[string]any) { turn["turn_kind"] = "unsupported" }},
	} {
		t.Run(test.name, func(t *testing.T) {
			candidate := validCombatResponse()
			test.mutate(candidate["combat_turns"].([]any)[0].(map[string]any))
			content, err := json.Marshal(candidate)
			if err != nil {
				t.Fatal(err)
			}
			if err := validateJSONSchema(content, schema.JSONSchema); err == nil {
				t.Fatal("private schema accepted structurally invalid response")
			}
		})
	}
}

func TestResponseSchemaAcceptsEverySupportedTurnKind(t *testing.T) {
	schema, err := loadResponseSchema()
	if err != nil {
		t.Fatal(err)
	}
	for _, kind := range []string{string(dnd.CombatTurnKindTurn), string(dnd.CombatTurnKindReaction), string(dnd.CombatTurnKindLegendaryAction), string(dnd.CombatTurnKindLairAction), string(dnd.CombatTurnKindOther)} {
		candidate := validCombatResponse()
		candidate["combat_turns"].([]any)[0].(map[string]any)["turn_kind"] = kind
		content, err := json.Marshal(candidate)
		if err != nil {
			t.Fatal(err)
		}
		if err := validateJSONSchema(content, schema.JSONSchema); err != nil {
			t.Fatalf("supported turn kind %q was rejected: %v", kind, err)
		}
	}
}

func TestResponseSchemaJSONIsMutationSafe(t *testing.T) {
	first, err := loadResponseSchema()
	if err != nil {
		t.Fatal(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.Fatal(err)
	}
	diagnostics := schema.DiagnosticsMap()
	if diagnostics["key"] != ResponseSchemaKey || diagnostics["id"] != ResponseSchemaID {
		t.Fatalf("diagnostics = %#v, want schema identity", diagnostics)
	}
	if _, ok := diagnostics["json_schema"]; ok {
		t.Fatalf("diagnostics include raw schema: %#v", diagnostics)
	}
}

func validCombatResponse() map[string]any {
	return map[string]any{
		"combat_turns": []any{
			map[string]any{
				"actor":       "Aria",
				"turn_kind":   "reaction",
				"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)
}
