package collection

import "time"

// Collection represents a collection (folder/album) stored locally with sync capabilities.
type Collection struct {
	// Identifiers (from cloud)
	ID       string `json:"id"`
	ParentID string `json:"parent_id,omitempty"`
	OwnerID  string `json:"owner_id"` // UserID from cloud

	// Encryption data (from cloud)
	EncryptedCollectionKey string `json:"encrypted_collection_key"`
	Nonce                  string `json:"nonce"`

	// Collection metadata (from cloud - name is decrypted client-side)
	Name        string `json:"name"`                  // Decrypted name
	Description string `json:"description,omitempty"` // Optional description
	// CustomIcon is the decrypted custom icon for this collection.
	// Empty string means use default folder/album icon.
	// Contains either an emoji character (e.g., "📷") or "icon:<identifier>" for predefined icons.
	CustomIcon string `json:"custom_icon,omitempty"`

	// Statistics (from cloud)
	TotalFiles       int   `json:"total_files"`
	TotalSizeInBytes int64 `json:"total_size_in_bytes"`

	// Sharing info (from cloud)
	PermissionLevel string `json:"permission_level,omitempty"` // read_only, read_write, admin
	IsOwner         bool   `json:"is_owner"`
	OwnerName       string `json:"owner_name,omitempty"`
	OwnerEmail      string `json:"owner_email,omitempty"`

	// Sync tracking (local only)
	SyncStatus   SyncStatus `json:"sync_status"`
	LastSyncedAt time.Time  `json:"last_synced_at,omitempty"`

	// State from cloud
	State string `json:"state"` // active, deleted

	// Timestamps (from cloud)
	CreatedAt  time.Time `json:"created_at"`
	ModifiedAt time.Time `json:"modified_at"`
}

// SyncStatus defines the synchronization status of a collection
type SyncStatus int

const (
	// SyncStatusCloudOnly indicates the collection metadata is synced from cloud
	SyncStatusCloudOnly SyncStatus = iota

	// SyncStatusSynced indicates the collection is fully synchronized
	SyncStatusSynced
)

// String returns a human-readable string representation of the sync status
func (s SyncStatus) String() string {
	switch s {
	case SyncStatusCloudOnly:
		return "cloud_only"
	case SyncStatusSynced:
		return "synced"
	default:
		return "unknown"
	}
}

// Collection state constants
const (
	// StateActive indicates the collection is active
	StateActive = "active"

	// StateDeleted indicates the collection is deleted
	StateDeleted = "deleted"
)

// Permission level constants
const (
	PermissionReadOnly  = "read_only"
	PermissionReadWrite = "read_write"
	PermissionAdmin     = "admin"
)

// IsDeleted returns true if the collection is marked as deleted
func (c *Collection) IsDeleted() bool {
	return c.State == StateDeleted
}

// CanWrite returns true if the user has write permissions
func (c *Collection) CanWrite() bool {
	return c.IsOwner || c.PermissionLevel == PermissionReadWrite || c.PermissionLevel == PermissionAdmin
}

// CanAdmin returns true if the user has admin permissions
func (c *Collection) CanAdmin() bool {
	return c.IsOwner || c.PermissionLevel == PermissionAdmin
}
