package ports

import (
	"context"
	"time"

	"github.com/nexuscrm/shared/pkg/models"
)

// MRUObjectInteraction represents a user's interaction with an object type
type MRUObjectInteraction struct {
	ID               string
	UserID           string
	ObjectAPIName    string
	InteractionCount int
	LastAccessedAt   time.Time
	IsPinned         bool
}

// MRURecentItem represents a user's recently accessed record
type MRURecentItem struct {
	ID            string
	UserID        string
	ObjectAPIName string
	RecordID      string
	AccessedAt    time.Time
	UpdateCounter int64
}

// MRURepository defines the interface for MRU (Most Recently Used) tracking persistence
// This follows the clean architecture principle: domain defines the interface,
// infrastructure provides the implementation.
type MRURepository interface {
	// TrackObjectInteraction upserts an object interaction record
	TrackObjectInteraction(ctx context.Context, userID, objectAPIName string) error

	// GetScopeOrder returns the user's search scope order (pinned first, then by interaction count)
	GetScopeOrder(ctx context.Context, userID string) ([]MRUObjectInteraction, error)

	// SetObjectPinned pins or unpins an object for a user
	SetObjectPinned(ctx context.Context, userID, objectAPIName string, pinned bool) error

	// TrackRecentItem upserts a recent item record
	TrackRecentItem(ctx context.Context, userID, objectAPIName, recordID string) error

	// GetRelevantItems returns the user's recently accessed records
	// Returns items and the max update counter for pagination
	GetRelevantItems(ctx context.Context, userID string, sobjects []string, lastUpdatedId int64) ([]MRURecentItem, int64, error)

	// GetObjectMeta retrieves object metadata for building response
	GetObjectMeta(ctx context.Context, objectAPIName string) (*models.ObjectMetadata, error)

	// GetObjectMetaBatch retrieves metadata for multiple objects in a single query
	GetObjectMetaBatch(ctx context.Context, objectAPINames []string) (map[string]*models.ObjectMetadata, error)

	// GetRecordNamesBatch retrieves record Names for multiple objects/records in batch
	// byObject is a map of objectAPIName -> []recordIDs
	// Returns a map of recordID -> Name
	GetRecordNamesBatch(ctx context.Context, byObject map[string][]string) (map[string]string, error)
}
