package graph

import "bit.admedia.com/scm/ad/adpilot-indexing-commit-intel.com/internal/contracts/metadata"

const DeltaSchemaVersionV1 = "v1"

// Delta captures graph changes between two commits for downstream analysis.
type Delta struct {
	SchemaVersion   string                     `json:"schema_version"`
	RepoID          string                     `json:"repo_id"`
	SnapshotID      string                     `json:"snapshot_id"`
	BaseCommitSHA   string                     `json:"base_commit_sha"`
	TargetCommitSHA string                     `json:"target_commit_sha"`
	ChangedFiles    []string                   `json:"changed_files"`
	FileDeltas      []FileDelta                `json:"file_deltas"`
	GitCommit       metadata.GitCommitMetadata `json:"git_commit,omitempty"`
}

// FileDelta describes graph changes for a single file between base and target artifacts.
type FileDelta struct {
	FilePath         string `json:"file_path"`
	BaseArtifactID   string `json:"base_artifact_id,omitempty"`
	TargetArtifactID string `json:"target_artifact_id,omitempty"`
	AddedNodes       []Node `json:"added_nodes,omitempty"`
	RemovedNodes     []Node `json:"removed_nodes,omitempty"`
	AddedEdges       []Edge `json:"added_edges,omitempty"`
	RemovedEdges     []Edge `json:"removed_edges,omitempty"`
}

// Counts returns aggregate node/edge change counts across all file deltas.
func (d Delta) Counts() (addedNodes, removedNodes, changedEdges int) {
	for _, fileDelta := range d.FileDeltas {
		addedNodes += len(fileDelta.AddedNodes)
		removedNodes += len(fileDelta.RemovedNodes)
		changedEdges += len(fileDelta.AddedEdges) + len(fileDelta.RemovedEdges)
	}
	return addedNodes, removedNodes, changedEdges
}
