package itemoccurrences

import (
	"context"
	"encoding/json"
	"errors"
	"testing"

	"gitea.maximumdirect.net/eric/notarius/internal/core/source"
	"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
)

func extractionRequest() contracts.TypedExtractionRequest {
	doc := sourceDocument()
	chunk := &source.Chunk{
		ID:        "session-alpha:chunk:0",
		SourceID:  doc.ID,
		Index:     0,
		Ref:       source.SourceRef{SourceID: doc.ID, StartUnitID: 1, EndUnitID: 5},
		Content:   []byte(`{"segments":[1,2,3,4,5]}`),
		MediaType: "application/json",
		Units:     append([]source.SourceUnit(nil), doc.Units...),
	}
	return contracts.TypedExtractionRequest{
		Source:      doc,
		Chunk:       chunk,
		SourceInput: contracts.NewLLMInputMaterial("source", chunk.MediaType, chunk.Content, "sha256:chunk", "file:///session-alpha.json"),
		SessionID:   "session-123",
		LLMProfile:  "profile-item-occurrences",
	}
}

func sourceDocument() *source.SourceDocument {
	return &source.SourceDocument{
		ID: "session-alpha", Kind: "transcript", Format: "application/vnd.seriatim.minimal+json", Digest: "sha256:test",
		Units: []source.SourceUnit{
			{ID: 1, Kind: "transcript_segment", Text: "The party discovers a hidden cache."},
			{ID: 2, Kind: "transcript_segment", Text: "They acquire 20 gold pieces."},
			{ID: 3, Kind: "transcript_segment", Text: "Aria gives Borin the moonblade."},
			{ID: 4, Kind: "transcript_segment", Text: "The party consumes a healing potion."},
			{ID: 5, Kind: "transcript_segment", Text: "The party loses a torch."},
		},
	}
}

func responseRefs(start, end int) []itemOccurrenceSourceRefResponse {
	return []itemOccurrenceSourceRefResponse{{StartUnitID: start, EndUnitID: end}}
}

func newExtractor(t *testing.T, client contracts.StructuredLLMClient, references ...contracts.ReferenceSet) *Extractor {
	t.Helper()
	extractor, err := New(client, Options{}, references...)
	if err != nil {
		t.Fatalf("New() error = %v", err)
	}
	return extractor
}

func cloneStructuredCompletionRequest(req contracts.StructuredCompletionRequest) contracts.StructuredCompletionRequest {
	req.Inputs = req.Inputs.Clone()
	correction, err := contracts.CloneSemanticCorrection(req.Correction)
	if err != nil {
		panic(err)
	}
	req.Correction = correction
	return req
}

type fakeItemOccurrencesLLMClient struct {
	response extractionResponse
	content  []byte
	err      error
	requests []contracts.StructuredCompletionRequest
}

func (client *fakeItemOccurrencesLLMClient) CompleteStructured(_ context.Context, req contracts.StructuredCompletionRequest, out any) (contracts.StructuredCompletionResponse, error) {
	client.requests = append(client.requests, cloneStructuredCompletionRequest(req))
	if client.err != nil {
		return contracts.StructuredCompletionResponse{}, client.err
	}
	target, ok := out.(*extractionResponse)
	if !ok {
		return contracts.StructuredCompletionResponse{}, errors.New("unexpected output target")
	}
	content := append([]byte(nil), client.content...)
	if len(content) == 0 {
		var err error
		content, err = json.Marshal(client.response)
		if err != nil {
			return contracts.StructuredCompletionResponse{}, err
		}
	}
	if err := json.Unmarshal(content, target); err != nil {
		return contracts.StructuredCompletionResponse{}, err
	}
	return contracts.StructuredCompletionResponse{Content: content}, nil
}
