package file

import "time"

// File represents a file stored locally with sync capabilities.
// This model combines cloud metadata with local storage tracking.
type File struct {
	// Identifiers (from cloud)
	ID           string `json:"id"`
	CollectionID string `json:"collection_id"`
	OwnerID      string `json:"owner_id"` // UserID from cloud

	// Encryption data (from cloud API response)
	EncryptedFileKey  EncryptedFileKeyData `json:"encrypted_file_key"`
	FileKeyNonce      string               `json:"file_key_nonce"`
	EncryptedMetadata string               `json:"encrypted_metadata"`
	MetadataNonce     string               `json:"metadata_nonce"`
	FileNonce         string               `json:"file_nonce"`

	// File sizes (from cloud)
	EncryptedSizeInBytes int64 `json:"encrypted_file_size_in_bytes"`
	DecryptedSizeInBytes int64 `json:"decrypted_size_in_bytes,omitempty"`

	// Local storage paths (local only)
	EncryptedFilePath string `json:"encrypted_file_path,omitempty"`
	FilePath          string `json:"file_path,omitempty"`
	ThumbnailPath     string `json:"thumbnail_path,omitempty"`

	// Decrypted metadata (local only - populated after decryption)
	Name     string        `json:"name,omitempty"`
	MimeType string        `json:"mime_type,omitempty"`
	Metadata *FileMetadata `json:"metadata,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"`        // pending, active, deleted
	StorageMode string `json:"storage_mode"` // encrypted_only, hybrid, decrypted_only
	Version     int    `json:"version"`      // Cloud version for conflict resolution

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

	// Thumbnail URL (from cloud, for remote access)
	ThumbnailURL string `json:"thumbnail_url,omitempty"`
}

// EncryptedFileKeyData matches the cloud API structure exactly
type EncryptedFileKeyData struct {
	Ciphertext string `json:"ciphertext"`
	Nonce      string `json:"nonce"`
}

// FileMetadata represents decrypted file metadata (populated after decryption)
type FileMetadata struct {
	Name          string `json:"name"`
	MimeType      string `json:"mime_type"`
	Size          int64  `json:"size"`
	FileExtension string `json:"file_extension"`
}

// IsCloudOnly returns true if the file only exists in the cloud
func (f *File) IsCloudOnly() bool {
	return f.SyncStatus == SyncStatusCloudOnly
}

// IsSynced returns true if the file is synchronized between local and cloud
func (f *File) IsSynced() bool {
	return f.SyncStatus == SyncStatusSynced
}

// IsLocalOnly returns true if the file only exists locally
func (f *File) IsLocalOnly() bool {
	return f.SyncStatus == SyncStatusLocalOnly
}

// HasLocalContent returns true if the file has local content (not just metadata)
func (f *File) HasLocalContent() bool {
	return f.FilePath != "" || f.EncryptedFilePath != ""
}

// IsDeleted returns true if the file is marked as deleted
func (f *File) IsDeleted() bool {
	return f.State == StateDeleted
}
