-- Migration 003: Workflow Engine
-- Creates tables for workflow definitions and execution tracking

-- Workflows table (workflow definitions)
DEFINE TABLE workflows SCHEMAFULL
    PERMISSIONS
        FOR select WHERE tenant_id = $auth.tenant_id
        FOR create, update, delete WHERE tenant_id = $auth.tenant_id AND ("admin" IN $auth.roles OR "project_manager" IN $auth.roles);

DEFINE FIELD id ON TABLE workflows TYPE record<workflows>;
DEFINE FIELD tenant_id ON TABLE workflows TYPE string ASSERT $value != NONE;
DEFINE FIELD name ON TABLE workflows TYPE string ASSERT $value != NONE;
DEFINE FIELD description ON TABLE workflows TYPE option<string>;
DEFINE FIELD status ON TABLE workflows TYPE string ASSERT $value INSIDE ["draft", "active", "paused", "completed", "failed"] DEFAULT "draft";
DEFINE FIELD definition ON TABLE workflows TYPE object DEFAULT {};
DEFINE FIELD created_at ON TABLE workflows TYPE datetime DEFAULT time::now();
DEFINE FIELD updated_at ON TABLE workflows TYPE datetime DEFAULT time::now() VALUE time::now();

DEFINE INDEX idx_workflows_tenant ON TABLE workflows COLUMNS tenant_id;
DEFINE INDEX idx_workflows_status ON TABLE workflows COLUMNS status;
DEFINE INDEX idx_workflows_tenant_status ON TABLE workflows COLUMNS tenant_id, status;

-- Workflow steps table (execution tracking)
DEFINE TABLE workflow_steps SCHEMAFULL
    PERMISSIONS
        FOR select WHERE $parent.tenant_id = $auth.tenant_id
        FOR create, update WHERE $parent.tenant_id = $auth.tenant_id;

DEFINE FIELD id ON TABLE workflow_steps TYPE record<workflow_steps>;
DEFINE FIELD workflow_id ON TABLE workflow_steps TYPE string ASSERT $value != NONE;
DEFINE FIELD step_id ON TABLE workflow_steps TYPE string ASSERT $value != NONE;
DEFINE FIELD step_name ON TABLE workflow_steps TYPE string ASSERT $value != NONE;
DEFINE FIELD agent_id ON TABLE workflow_steps TYPE option<string>;
DEFINE FIELD status ON TABLE workflow_steps TYPE string ASSERT $value INSIDE ["pending", "in_progress", "completed", "failed", "skipped"] DEFAULT "pending";
DEFINE FIELD result ON TABLE workflow_steps TYPE option<object>;
DEFINE FIELD error_message ON TABLE workflow_steps TYPE option<string>;
DEFINE FIELD started_at ON TABLE workflow_steps TYPE option<datetime>;
DEFINE FIELD completed_at ON TABLE workflow_steps TYPE option<datetime>;
DEFINE FIELD created_at ON TABLE workflow_steps TYPE datetime DEFAULT time::now();

DEFINE INDEX idx_workflow_steps_workflow ON TABLE workflow_steps COLUMNS workflow_id;
DEFINE INDEX idx_workflow_steps_status ON TABLE workflow_steps COLUMNS status;
DEFINE INDEX idx_workflow_steps_agent ON TABLE workflow_steps COLUMNS agent_id;
DEFINE INDEX idx_workflow_steps_workflow_step ON TABLE workflow_steps COLUMNS workflow_id, step_id UNIQUE;

-- Workflow executions table (execution history)
DEFINE TABLE workflow_executions SCHEMAFULL
    PERMISSIONS
        FOR select WHERE tenant_id = $auth.tenant_id
        FOR create WHERE tenant_id = $auth.tenant_id;

DEFINE FIELD id ON TABLE workflow_executions TYPE record<workflow_executions>;
DEFINE FIELD tenant_id ON TABLE workflow_executions TYPE string ASSERT $value != NONE;
DEFINE FIELD workflow_id ON TABLE workflow_executions TYPE string ASSERT $value != NONE;
DEFINE FIELD status ON TABLE workflow_executions TYPE string ASSERT $value INSIDE ["running", "completed", "failed", "cancelled"] DEFAULT "running";
DEFINE FIELD started_at ON TABLE workflow_executions TYPE datetime DEFAULT time::now();
DEFINE FIELD completed_at ON TABLE workflow_executions TYPE option<datetime>;
DEFINE FIELD duration_ms ON TABLE workflow_executions TYPE option<int>;

DEFINE INDEX idx_workflow_executions_workflow ON TABLE workflow_executions COLUMNS workflow_id;
DEFINE INDEX idx_workflow_executions_tenant ON TABLE workflow_executions COLUMNS tenant_id;
DEFINE INDEX idx_workflow_executions_status ON TABLE workflow_executions COLUMNS status;
