package parser

import (
	"context"

	contractevents "bit.admedia.com/scm/ad/adpilot-indexing-code-parser.com/internal/contracts/events"
	"bit.admedia.com/scm/ad/adpilot-indexing-code-parser.com/internal/contracts/graph"
)

// IndexingRunSnapshot summarizes fan-in counters for snapshot finalization.
type IndexingRunSnapshot struct {
	RepoID             string
	SnapshotID         string
	CommitSHA          string
	ExpectedCodeFiles  int
	ExpectedDocsFiles  int
	ProcessedCodeFiles int
	ProcessedDocsFiles int
	FailedFiles        int
}

// DocChunkRecord is a documentation chunk used during graph merge.
type DocChunkRecord struct {
	ChunkID      string
	DocPath      string
	SectionTitle string
	ChunkText    string
}

// DocSectionRecord is a documentation section used during graph merge.
type DocSectionRecord struct {
	SectionID    string
	DocPath      string
	SectionTitle string
}

// FinalizeStore provides Mongo-backed snapshot finalization operations.
type FinalizeStore interface {
	SaveCodeChunks(ctx context.Context, repoID, snapshotID, commitSHA, filePath, language, artifactID string, chunks []contractevents.CodeChunk) error
	SaveIndexingDiagnostics(ctx context.Context, repoID, snapshotID, stage, sourceType, sourceID, jobID, eventID string, diagnostics []contractevents.ParserDiagnostic) error
	ListArtifactsBySnapshot(ctx context.Context, repoID, snapshotID string) ([]graph.Artifact, error)
	SaveSnapshotGraph(ctx context.Context, artifact graph.Artifact, artifactID string) (string, string, error)
	GetSnapshotGraphBySnapshot(ctx context.Context, repoID, snapshotID string) (graph.Artifact, string, bool, error)
	IncrementProcessedCodeFiles(ctx context.Context, repoID, snapshotID, filePath string) error
	IncrementSkippedCodeFiles(ctx context.Context, repoID, snapshotID, filePath string) error
	IncrementFailedCodeFiles(ctx context.Context, repoID, snapshotID, filePath string) error
	RecordCommitChangedFailure(ctx context.Context, repoID, snapshotID, commitSHA, eventID, message string) error
	CountProcessedCodeFilePaths(ctx context.Context, repoID, snapshotID string) (int, error)
	CountSkippedCodeFilePaths(ctx context.Context, repoID, snapshotID string) (int, error)
	CountSnapshotCodeFiles(ctx context.Context, repoID, snapshotID string) (int, error)
	CountCodeArtifacts(ctx context.Context, repoID, snapshotID string) (int, error)
	SyncCodeParseCounter(ctx context.Context, repoID, snapshotID string, processed int) error
	SyncCodeParseFanIn(ctx context.Context, repoID, snapshotID string, failureThreshold float64) error
	CountCompletedDocParseRuns(ctx context.Context, repoID, snapshotID string) (int, error)
	SyncDocsParseCounter(ctx context.Context, repoID, snapshotID string, processed int) error
	GetIndexingRun(ctx context.Context, repoID, snapshotID string) (IndexingRunSnapshot, error)
	SetGraphFinalizeCompleted(ctx context.Context, repoID, snapshotID string) error
	ListDocChunks(ctx context.Context, repoID, snapshotID string) ([]DocChunkRecord, error)
	ListDocSections(ctx context.Context, repoID, snapshotID string) ([]DocSectionRecord, error)
}

// CommitOrderStore enforces ordered commits.changed processing.
type CommitOrderStore interface {
	ParentCommitReady(ctx context.Context, repoID, snapshotID, parentSHA string) (bool, error)
	MarkCommitCompleted(ctx context.Context, repoID, snapshotID, commitSHA, parentSHA string) error
	SyncCommitFanIn(ctx context.Context, repoID, snapshotID string) error
}
