/**
 * Workflow Service (Flow Studio)
 */

import { getDb } from '../db/index.js'
import { logger } from '../lib/logger.js'

export interface Workflow {
  id: string
  name: string
  definition: any
  status: string
}

export class WorkflowService {
  async createWorkflow(name: string, definition: any): Promise<Workflow> {
    const db = getDb()
    const result = await db.query(
      `INSERT INTO workflows (name, definition, status) VALUES ($1, $2, $3) RETURNING *`,
      [name, JSON.stringify(definition), 'DRAFT']
    )
    return result.rows[0]
  }

  async compileToTemporal(workflow: Workflow): Promise<string> {
    logger.info('Compiling workflow to Temporal', { workflowId: workflow.id })
    // Compile flow to Temporal workflow definition
    return 'temporal-workflow-definition'
  }
}

export const workflowService = new WorkflowService()

