package db

import (
	"time"
)

// WorkflowNode represents a React Flow node in the canvas
type WorkflowNode struct {
	ID       string                 `json:"id"`
	Label    string                 `json:"label"`
	Type     string                 `json:"type"` // "activity"
	Position map[string]interface{} `json:"position"`
	Data     map[string]interface{} `json:"data"`
}

// WorkflowEdge represents a React Flow edge in the canvas
type WorkflowEdge struct {
	ID     string                 `json:"id"`
	Source string                 `json:"source"`
	Target string                 `json:"target"`
	Data   map[string]interface{} `json:"data"`
}

// Canvas represents the full React Flow canvas (nodes + edges)
type Canvas struct {
	Name  string         `json:"name,omitempty"`
	Nodes []WorkflowNode `json:"nodes"`
	Edges []WorkflowEdge `json:"edges"`
}

// Workflow represents a workflow definition in the database
type Workflow struct {
	ID            string    `db:"id"`
	CustomerID    string    `db:"customer_id"`
	Name          string    `db:"name"`
	Description   string    `db:"description"`
	Status        string    `db:"status"`        // "draft", "active", "archived"
	Version       int       `db:"version"`
	Nodes         []byte    `db:"nodes"`         // JSONB stored as []byte
	Edges         []byte    `db:"edges"`         // JSONB stored as []byte
	CreatedBy     string    `db:"created_by"`
	CreatedAt     time.Time `db:"created_at"`
	UpdatedAt     time.Time `db:"updated_at"`
	LastExecutedAt *time.Time `db:"last_executed_at"`
}

// WorkflowExecution represents a workflow execution run
type WorkflowExecution struct {
	ID           string    `db:"id"`
	WorkflowID   string    `db:"workflow_id"`
	CustomerID   string    `db:"customer_id"`
	TemporalID   string    `db:"temporal_id"` // Temporal execution ID
	Status       string    `db:"status"`      // "pending", "running", "success", "failed", "cancelled"
	Inputs       []byte    `db:"inputs"`      // JSONB
	Outputs      []byte    `db:"outputs"`     // JSONB
	StartedAt    time.Time `db:"started_at"`
	CompletedAt  *time.Time `db:"completed_at"`
	DurationMs   *int      `db:"duration_ms"`
	ErrorMessage string    `db:"error_message"`
	ErrorCount   int       `db:"error_count"`
}

// ExecutionLog represents a detailed activity log entry
type ExecutionLog struct {
	ID           int64     `db:"id"`
	ExecutionID  string    `db:"execution_id"`
	NodeID       string    `db:"node_id"`      // From canvas node ID
	ActivityName string    `db:"activity_name"` // "CloneRepo", "AnalyzeCode", etc
	Level        string    `db:"level"`        // "info", "warn", "error", "debug"
	Message      string    `db:"message"`
	Metadata     []byte    `db:"metadata"`    // JSONB
	LoggedAt     time.Time `db:"logged_at"`
}

// ActivityTrace represents per-activity execution metrics
type ActivityTrace struct {
	ID           int64      `db:"id"`
	ExecutionID  string     `db:"execution_id"`
	NodeID       string     `db:"node_id"`
	ActivityName string     `db:"activity_name"`
	Parameters   []byte     `db:"parameters"` // JSONB
	Result       []byte     `db:"result"`     // JSONB
	StartedAt    time.Time  `db:"started_at"`
	CompletedAt  *time.Time `db:"completed_at"`
	DurationMs   *int       `db:"duration_ms"`
	Attempt      int        `db:"attempt"`
	RetryReason  string     `db:"retry_reason"`
	Status       string     `db:"status"` // "running", "success", "failed", "skipped"
	ErrorMessage string     `db:"error_message"`
}

// WorkflowStats represents aggregated workflow metrics
type WorkflowStats struct {
	WorkflowID        string    `db:"workflow_id"`
	CustomerID        string    `db:"customer_id"`
	TotalRuns         int       `db:"total_runs"`
	SuccessfulRuns    int       `db:"successful_runs"`
	FailedRuns        int       `db:"failed_runs"`
	AvgDurationMs     float64   `db:"avg_duration_ms"`
	MinDurationMs     *int      `db:"min_duration_ms"`
	MaxDurationMs     *int      `db:"max_duration_ms"`
	Last30dRuns       int       `db:"last_30d_runs"`
	Last30dSuccessRate float64  `db:"last_30d_success_rate"`
	UpdatedAt         time.Time `db:"updated_at"`
}

// WorkflowMemoryLink represents a connection between execution and memory nodes
type WorkflowMemoryLink struct {
	ExecutionID   string    `db:"execution_id"`
	MemoryNodeSha string    `db:"memory_node_sha"`
	Relationship  string    `db:"relationship"` // "generated", "used", "learned", "failed_on"
	CreatedAt     time.Time `db:"created_at"`
	Notes         string    `db:"notes"`
}

// WorkflowRelation represents a semantic relation between two canvas nodes
type WorkflowRelation struct {
	ID               string    `db:"id"`
	WorkflowID       string    `db:"workflow_id"`
	Version          int       `db:"version"`
	SourceNodeID     string    `db:"source_node_id"`
	TargetNodeID     string    `db:"target_node_id"`
	RelationType     string    `db:"relation_type"` // "data-flow", "dependency", "conditional"
	Label            string    `db:"label"`         // Human-readable relation description
	RelationWording  []byte    `db:"relation_wording"`  // JSONB with verb, outputs, inputs, confidence
	Metadata         []byte    `db:"metadata"`      // JSONB for extensibility
	CreatedAt        time.Time `db:"created_at"`
}

// WorkflowRelationVersion represents versioned history of relation changes
type WorkflowRelationVersion struct {
	ID            string    `db:"id"`
	WorkflowID    string    `db:"workflow_id"`
	EdgeID        string    `db:"edge_id"`
	VersionNum    int       `db:"version_num"`
	Operation     string    `db:"operation"` // "CREATE", "UPDATE", "DELETE"
	Snapshot      []byte    `db:"snapshot"` // JSONB full state at this version
	ChangedAt     time.Time `db:"changed_at"`
	ChangedBy     string    `db:"changed_by"`
	FieldsChanged []byte    `db:"fields_changed"` // JSONB array of changed field names
}
