package syncstate

import "time"

// SyncState tracks the synchronization progress for collections and files.
// It stores cursors from the API for incremental sync and timestamps for tracking.
type SyncState struct {
	// Timestamps for tracking when sync occurred
	LastCollectionSync time.Time `json:"last_collection_sync"`
	LastFileSync       time.Time `json:"last_file_sync"`

	// Cursors from API responses (used for pagination)
	CollectionCursor string `json:"collection_cursor,omitempty"`
	FileCursor       string `json:"file_cursor,omitempty"`

	// Sync completion flags
	CollectionSyncComplete bool `json:"collection_sync_complete"`
	FileSyncComplete       bool `json:"file_sync_complete"`
}

// NewSyncState creates a new empty SyncState
func NewSyncState() *SyncState {
	return &SyncState{}
}

// IsCollectionSyncComplete returns true if all collections have been synced
func (s *SyncState) IsCollectionSyncComplete() bool {
	return s.CollectionSyncComplete
}

// IsFileSyncComplete returns true if all files have been synced
func (s *SyncState) IsFileSyncComplete() bool {
	return s.FileSyncComplete
}

// IsFullySynced returns true if both collections and files are fully synced
func (s *SyncState) IsFullySynced() bool {
	return s.CollectionSyncComplete && s.FileSyncComplete
}

// ResetCollectionSync resets the collection sync state for a fresh sync
func (s *SyncState) ResetCollectionSync() {
	s.CollectionCursor = ""
	s.CollectionSyncComplete = false
	s.LastCollectionSync = time.Time{}
}

// ResetFileSync resets the file sync state for a fresh sync
func (s *SyncState) ResetFileSync() {
	s.FileCursor = ""
	s.FileSyncComplete = false
	s.LastFileSync = time.Time{}
}

// Reset resets both collection and file sync states
func (s *SyncState) Reset() {
	s.ResetCollectionSync()
	s.ResetFileSync()
}

// UpdateCollectionSync updates the collection sync state after a sync operation
func (s *SyncState) UpdateCollectionSync(cursor string, hasMore bool) {
	s.CollectionCursor = cursor
	s.CollectionSyncComplete = !hasMore
	if !hasMore {
		s.LastCollectionSync = time.Now()
	}
}

// UpdateFileSync updates the file sync state after a sync operation
func (s *SyncState) UpdateFileSync(cursor string, hasMore bool) {
	s.FileCursor = cursor
	s.FileSyncComplete = !hasMore
	if !hasMore {
		s.LastFileSync = time.Now()
	}
}
