package connector_syncstate

import (
	"database/sql"
	"errors"
	"time"
)

// SyncState is per-(namespace, object, direction) cursor + error tracking state
// consumed by connector workers and surfaced by the connector health endpoint.
type SyncState struct {
	ID                string
	InstalledPackId   string // per-incarnation _System_InstalledPack.PackInstallId
	Namespace         string
	Object            string
	Direction         string
	Cursor            string // empty when no cursor written yet
	LastSyncAt        sql.NullTime
	LastErrorMessage  string
	LastErrorAt       sql.NullTime
	RunningErrorCount int
	IsActive          bool
	CreatedDate       time.Time
	LastModifiedDate  time.Time
}

// Direction enum — same neutral 3-value enum used by INT-06; CRM
// record-conversion semantics do NOT apply to SyncState.
const (
	DirectionInbound       = "inbound"
	DirectionOutbound      = "outbound"
	DirectionBidirectional = "bidirectional"
)

var (
	// ErrNotFound is returned when no SyncState row exists for the requested
	// (namespace, object, direction) tuple.
	ErrNotFound = errors.New("sync state not found")
	// ErrInvalidDirection is returned for a Direction value outside the enum.
	ErrInvalidDirection = errors.New("sync state direction must be inbound, outbound, or bidirectional")
	// ErrConnectorPackInactive is returned when ResolveConnectorPackByNamespace
	// reports the pack is missing, soft-deleted, inactive, or non-connector.
	// Worker writes to inactive packs would silently corrupt SyncState rows
	// across pack-rebirth; fail closed.
	ErrConnectorPackInactive = errors.New("connector pack inactive or unknown namespace")
)
