package models

import "time"

// =========================================
// WORKSPACES (v0.21.0)
// =========================================

// Workspace owner type constants.
const (
	WorkspaceOwnerUser    = "user"
	WorkspaceOwnerProject = "project"
	WorkspaceOwnerChannel = "channel"
	WorkspaceOwnerTeam    = "team"
)

// Workspace status constants.
const (
	WorkspaceStatusActive   = "active"
	WorkspaceStatusArchived = "archived"
	WorkspaceStatusDeleting = "deleting"
)

// Workspace represents a file workspace bound to an owner (user, project, channel, or team).
type Workspace struct {
	BaseModel
	OwnerType string  `json:"owner_type" db:"owner_type"`
	OwnerID   string  `json:"owner_id" db:"owner_id"`
	Name      string  `json:"name" db:"name"`
	RootPath  string  `json:"-" db:"root_path"` // never expose filesystem path
	MaxBytes  *int64  `json:"max_bytes,omitempty" db:"max_bytes"`
	Status    string  `json:"status" db:"status"`

	IndexingEnabled bool `json:"indexing_enabled" db:"indexing_enabled"`

	// Git integration (v0.21.4)
	GitRemoteURL    *string    `json:"git_remote_url,omitempty" db:"git_remote_url"`
	GitBranch       *string    `json:"git_branch,omitempty" db:"git_branch"`
	GitCredentialID *string    `json:"git_credential_id,omitempty" db:"git_credential_id"`
	GitLastSync     *time.Time `json:"git_last_sync,omitempty" db:"git_last_sync"`

	// Computed fields (not DB columns)
	FileCount  int   `json:"file_count,omitempty"`
	TotalBytes int64 `json:"total_bytes,omitempty"`
}

// WorkspacePatch holds optional fields for updating a workspace.
type WorkspacePatch struct {
	Name            *string `json:"name,omitempty"`
	MaxBytes        *int64  `json:"max_bytes,omitempty"`
	Status          *string `json:"status,omitempty"`
	IndexingEnabled *bool   `json:"indexing_enabled,omitempty"`

	// Git integration (v0.21.4)
	GitRemoteURL    *string `json:"git_remote_url,omitempty"`
	GitBranch       *string `json:"git_branch,omitempty"`
	GitCredentialID *string `json:"git_credential_id,omitempty"`
}

// WorkspaceFile represents metadata for a single file or directory in a workspace.
type WorkspaceFile struct {
	ID          string    `json:"id" db:"id"`
	WorkspaceID string    `json:"workspace_id" db:"workspace_id"`
	Path        string    `json:"path" db:"path"`
	IsDirectory bool      `json:"is_directory" db:"is_directory"`
	ContentType string    `json:"content_type,omitempty" db:"content_type"`
	SizeBytes   int64     `json:"size_bytes" db:"size_bytes"`
	SHA256      string    `json:"sha256,omitempty" db:"sha256"`
	IndexStatus string    `json:"index_status,omitempty" db:"index_status"`
	ChunkCount  int       `json:"chunk_count,omitempty" db:"chunk_count"`
	CreatedAt   time.Time `json:"created_at" db:"created_at"`
	UpdatedAt   time.Time `json:"updated_at" db:"updated_at"`
}

// WorkspaceStats holds aggregate storage metrics for a workspace.
type WorkspaceStats struct {
	WorkspaceID string `json:"workspace_id"`
	FileCount   int    `json:"file_count"`
	DirCount    int    `json:"dir_count"`
	TotalBytes  int64  `json:"total_bytes"`
	MaxBytes    *int64 `json:"max_bytes,omitempty"`
}

// =========================================
// WORKSPACE CHUNKS (v0.21.2)
// =========================================

// Workspace file index status constants.
const (
	IndexStatusPending  = "pending"
	IndexStatusIndexing = "indexing"
	IndexStatusReady    = "ready"
	IndexStatusError    = "error"
	IndexStatusSkipped  = "skipped"
)

// WorkspaceChunk is a text segment from a workspace file with its embedding vector.
type WorkspaceChunk struct {
	ID          string    `json:"id" db:"id"`
	WorkspaceID string    `json:"workspace_id" db:"workspace_id"`
	FileID      string    `json:"file_id" db:"file_id"`
	ChunkIndex  int       `json:"chunk_index" db:"chunk_index"`
	Content     string    `json:"content" db:"content"`
	TokenCount  int       `json:"token_count" db:"token_count"`
	Embedding   []float64 `json:"-" db:"embedding"` // never serialized to API
	Metadata    JSONMap   `json:"metadata" db:"metadata"`
}

// WorkspaceChunkResult is a single result from workspace similarity search.
type WorkspaceChunkResult struct {
	FilePath   string  `json:"file_path"`
	Content    string  `json:"content"`
	Score      float64 `json:"score"`
	LineHint   int     `json:"line_hint,omitempty"`
	Metadata   JSONMap `json:"metadata,omitempty"`
}
