package store

import (
	"context"
	"errors"

	"bit.admedia.com/scm/ad/adpilot-indexing-repo-sync.com/internal/domain"
)

// ErrDuplicateImportJob is returned when the same importJobId is submitted twice.
var ErrDuplicateImportJob = errors.New("duplicate import job")

// MetadataStore persists repository, snapshot, and sync job metadata.
type MetadataStore interface {
	Ready(ctx context.Context) bool
	Close(ctx context.Context) error
	EnsureIndexes(ctx context.Context) error

	UpsertRepository(ctx context.Context, repo domain.Repository) error
	GetRepository(ctx context.Context, repoID string) (domain.Repository, bool, error)
	ListRepositories(ctx context.Context) ([]domain.Repository, error)

	UpsertOrgRepositoryLink(ctx context.Context, link domain.OrgRepositoryLink) error
	ListRepoIDsByOrg(ctx context.Context, orgID string) ([]string, error)

	SaveSnapshot(ctx context.Context, snap domain.Snapshot) error
	GetSnapshot(ctx context.Context, repoID, snapshotID string) (domain.Snapshot, bool, error)
	GetLatestSnapshot(ctx context.Context, repoID string) (domain.Snapshot, bool, error)

	CreateSyncJob(ctx context.Context, job domain.SyncJob) error
	UpdateSyncJob(ctx context.Context, job domain.SyncJob) error

	CreateImportJob(ctx context.Context, job domain.ImportJob) error
	GetImportJob(ctx context.Context, importJobID string) (domain.ImportJob, bool, error)

	CreateIndexingRun(ctx context.Context, run domain.IndexingRun) error
	UpdateIndexingRun(ctx context.Context, run domain.IndexingRun) error
	GetIndexingRun(ctx context.Context, indexingRunID string) (domain.IndexingRun, bool, error)
	GetIndexingRunBySnapshot(ctx context.Context, repoID, snapshotID string) (domain.IndexingRun, bool, error)
	GetLatestIndexingRunByRepo(ctx context.Context, repoID string) (domain.IndexingRun, bool, error)

	SaveIndexingDiagnostic(ctx context.Context, diagnostic domain.IndexingDiagnostic) error
	ListIndexingDiagnostics(ctx context.Context, repoID, snapshotID string, limit int) ([]domain.IndexingDiagnostic, error)

	InsertSnapshotFiles(ctx context.Context, files []domain.SnapshotFile) error
	ListSnapshotFiles(ctx context.Context, repoID, snapshotID string) ([]domain.SnapshotFile, error)
}
