package models

import (
	"time"
)

// WorkflowTemplate - JSON-configurable workflow definition (admin-managed)
type WorkflowTemplate struct {
	ID          uint   `gorm:"primaryKey"`
	Name        string `gorm:"size:200;not null;uniqueIndex"`
	Description string `gorm:"type:text"`
	Definition  string `gorm:"type:text;not null"` // JSON DAG definition
	IsActive    bool   `gorm:"default:true"`
	CreatedByID uint
	CreatedBy   User `gorm:"foreignKey:CreatedByID"`
	CreatedAt   time.Time
	UpdatedAt   time.Time
}

// WorkflowInstance - a running instance of a workflow (many-to-one with Service)
type WorkflowInstance struct {
	ID                 uint `gorm:"primaryKey"`
	WorkflowTemplateID uint
	WorkflowTemplate   WorkflowTemplate `gorm:"foreignKey:WorkflowTemplateID"`
	Status             string           `gorm:"size:50;not null"` // "running", "completed", "failed", "paused"
	Context            string           `gorm:"type:text"`        // JSON workflow variables
	ServiceID          *uint            // Many-to-one: multiple workflows can belong to one service
	Service            *Service         `gorm:"foreignKey:ServiceID"`
	CreatedAt          time.Time
	UpdatedAt          time.Time
	CompletedAt        *time.Time
}

// WorkflowNode - represents a task node in a workflow instance
type WorkflowNode struct {
	ID                 uint `gorm:"primaryKey"`
	WorkflowInstanceID uint
	WorkflowInstance   WorkflowInstance `gorm:"foreignKey:WorkflowInstanceID"`
	NodeKey            string           `gorm:"size:100;not null"`
	NodeType           string           `gorm:"size:50;not null"` // "task", "condition", "parallel", "join"
	Status             string           `gorm:"size:50;not null"` // "pending", "ready", "running", "completed", "skipped", "failed"
	TaskID             *uint            // Link to created task (assignment is on Task)
	Task               *Task            `gorm:"foreignKey:TaskID"`
	RetryCount         int              `gorm:"default:0"`
	InputData          string           `gorm:"type:text"`
	OutputData         string           `gorm:"type:text"`
	CreatedAt          time.Time
	UpdatedAt          time.Time
	StartedAt          *time.Time
	CompletedAt        *time.Time
}

// WorkflowEdge - dependency between nodes
type WorkflowEdge struct {
	ID                 uint `gorm:"primaryKey"`
	WorkflowInstanceID uint
	FromNodeID         uint
	FromNode           WorkflowNode `gorm:"foreignKey:FromNodeID"`
	ToNodeID           uint
	ToNode             WorkflowNode `gorm:"foreignKey:ToNodeID"`
	Condition          string       `gorm:"type:text"`
}
