package vcs

import (
	
	
)

// Revision identifies a resolved VCS commit and optional movable reference
// (such as a Git branch, Mercurial bookmark/branch, Fossil branch, or Jujutsu
// bookmark).
type Revision struct {
	// ID is the immutable revision identifier (e.g. a commit hash).
	ID string `json:"id"`
	// Ref is the backend-specific canonical ref name, if any.
	Ref string `json:"ref,omitzero"`
	// Label is the human-friendly ref name (e.g. "main", "default", "trunk").
	Label string `json:"label,omitzero"`
}

// FileEntry describes a single tracked file in a VCS tree.
type FileEntry struct {
	// Path is the repository-relative slash-separated path.
	Path string
	// Mode is the file mode (notably executable bit 0o111).
	Mode fs.FileMode
}

// CommitOptions configures a VCS-agnostic commit. Backend-specific options
// (such as GPG/SSH signing) live in each backend’s own configuration.
type CommitOptions struct {
	Message string
}

// TagOptions configures a VCS-agnostic tag. Backend-specific options (such as
// annotated vs lightweight tags or tag commit messages) live in each backend’s
// own configuration.
type TagOptions struct {
	Message string
}

// PublishResult summarizes the commits and tags finalized by Session.Publish.
type PublishResult struct {
	StartID  string   `json:"startId"`
	FinalID  string   `json:"finalId"`
	Ref      string   `json:"ref,omitzero"`
	Label    string   `json:"label,omitzero"`
	Tags     []string `json:"tags,omitzero"`
	PushHint string   `json:"pushHint,omitzero"`
}

// VCS provides repository inspection, transactional release sessions, and push
// operations for a specific version control system.
type VCS interface {
	// Name returns the backend identifier (e.g. "git", "hg", "fossil", "jj").
	Name() string

	// Root returns the repository root identifier (typically an absolute path).
	Root() string

	// ResolveRev resolves a revision expression. An empty rev resolves the
	// backend’s current working revision (e.g. HEAD in Git, "." in Mercurial,
	// "@" in Jujutsu, "current" in Fossil).
	ResolveRev(ctx context.Context, rev string) (Revision, error)

	// Tags returns all tags currently present in the repository.
	Tags(ctx context.Context) ([]string, error)

	// ChangedFiles returns repo-relative slash paths changed between baseRev
	// and targetRev under dir ("" or "." for the whole repository).
	ChangedFiles(ctx context.Context, baseRev, targetRev, dir string) ([]string, error)

	// ListFiles returns all VCS-tracked files at rev under dir.
	ListFiles(ctx context.Context, rev, dir string) ([]FileEntry, error)

	// ReadFile returns the contents of a VCS-tracked file at rev.
	ReadFile(ctx context.Context, rev, slashPath string) ([]byte, error)

	// Begin starts an isolated release transaction rooted at rev.
	Begin(ctx context.Context, rev Revision) (Session, error)

	// Push pushes the updated ref (if non-empty) and tags to remote. An empty
	// remote uses the backend’s default remote.
	Push(ctx context.Context, remote, ref string, tags []string) error

	// PushHint returns a human-readable command hint for publishing res to
	// remote when -push was not passed.
	PushHint(remote string, res PublishResult) string
}

// Exporter is an optional interface that VCS backends may implement to export
// the full tracked file tree at rev into destDir in a single operation (for
// example via "git archive" or "hg archive") rather than per-file reads.
type Exporter interface {
	Export(ctx context.Context, rev, destDir string) error
}

// Session represents an isolated transactional workspace for creating release
// commits and tags without directly exposing local filesystem paths to callers.
type Session interface {
	// FS returns a read-only filesystem view of the session’s working tree.
	FS() fs.FS

	// WriteFile writes data to a repo-relative slash path in the session’s
	// working tree.
	WriteFile(slashPath string, data []byte) error

	// Head returns the current revision ID inside this session.
	Head(ctx context.Context) (string, error)

	// ListFiles returns all VCS-tracked files at the session’s current HEAD
	// under dir ("" or "." for the whole repository).
	ListFiles(ctx context.Context, dir string) ([]FileEntry, error)

	// Commit stages files (repo-relative slash paths) and creates a commit.
	Commit(ctx context.Context, files []string, opts CommitOptions) (string, error)

	// Tag records a tag at the session’s current HEAD.
	Tag(ctx context.Context, name string, opts TagOptions) error

	// Publish finalizes all commits and tags into the repository.
	Publish(ctx context.Context) (PublishResult, error)

	// Close releases temporary resources. Calling Close without Publish
	// leaves the repository unmodified.
	Close() error
}